Sometimes my program aborts with
fatal.nim(49) sysFatal
Error: unhandled exception: index -1 not in 0 .. 12 [IndexDefect]
I am not handling range exceptions correctly, it is a two part question: how to trap exceptions of this type and, how to determine where in my code it is occurring.
Thus when zero matches (a rare condition) it ends up as 0..-1 and causes the exception
Why would it? The loop would not be run.
Even if syntax sugar were available I would recommend the more verbose approach, simply for clarity.
let matchCount = customregex() # return number of matches
let hasMatches = matchCount > 0
if hasMatches:
for i in 0 .. matchCount:
This would honestly be more elegant though if customregex didn't return the number of matches, but the matches themselves. What you actually want to express is to do X per match, right? So imo what you actually seem to want is a for _ in matches: loop.
Just to expand on Araq's answer. It doesn't look like this defect is raised by a for loop, because in Nim, in case where right boundary is less than left e.g. 0 .. -1 loop block would never run:
let zero = 0
let c = zero
for i in 0 .. c - 1:
echo "You would never see this."
Perhaps somewhere in your code you access array or seq of 13 elements with an index that could be less than 0?
var Arr: array[13, int]
var Seq: seq[int] = @[0,1,2,3,4,5,6,7,8,9,10,11,12]
let zero = 0
let i = zero
echo Arr[i-1] # either of these two will give you
echo Seq[i-1] # Error: unhandled exception: index -1 not in 0 .. 12 [IndexDefect]
@stbalbach
I'll keep looking. It won't be easy. A lot of code and files. Including tables.
You may want to use nim-gdb after compiling with debug options. It helps!
for index, _ in sequence: doSomethingUsingIndex
?