type PageNo = 1 .. 100
var a: PageNo = 1
a = if a == PageNo.low(): a else: (a - 1)
a = if a == PageNo.low(): a else: (a - 1)
echo a
One way I can think of is using max
type PageNo = 1 .. 100
var a: PageNo = 1
a = max(PageNo.low, a - 1)
a = max(PageNo.low, a - 1)
echo a
Honestly your approach of "first check, then decrement" is working well enough. You can just refactor it for better readability. E.g.:
type PageNo = 1 .. 100
proc isFirstPage(x: PageNo): bool =
x == PageNo.low()
proc dec(x: var PageNo) =
x = x - 1
var a: PageNo = 1
if not a.isFirstPage():
a.dec
if not a.isFirstPage():
a.dec
echo a
Though that is more to stay true to your original design. You can also just move it all into dec to make it not error for bad inputs, which appears to be your intent:
type PageNo = 1 .. 100
proc isFirstPage(x: PageNo): bool =
x == PageNo.low()
proc dec(pageNum: var PageNo) =
if not pageNum.isFirstPage():
pageNum = pageNum - 1
var a: PageNo = 1
a.dec
a.dec
echo a