Thanks for the pointer. I adapted the example like this:
const DIGITS = "0123456789"
proc parseInt(s : string, radix: range[8..10]) : uint64 =
var str = s
result = 0
for i in 0 .. str.high:
let c = str[i]
assert c in DIGITS[0 ..< radix]
result = result * radix + uint64(DIGITS.find c)
echo parseInt("077", 8)
It compiles, and seems to run as expected, but I get a warning which is hard to understand:
Hint: used config file '/nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: in [Processing]
in.nim(9, 14) template/generic instantiation from here
lib/system.nim(325, 3) Warning: Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]
It's complaining about the assert line - but I can't see the connection between that and result, and moreover result is initialized at line 5, result = 0. Any ideas how to interpret this?The message is misleading. "result" is the one at line 325 in system.nim, i.e. the HSlice which is returned by the proc.
When instanciating the slice (".."), the fields "a" and "b" of "result" (a slice) should be initialized with 0. But "b" is of type "range[8..10] and cannot be initialized with this value. So, the compiler emits a warning.
To avoid this warning, you simply can write "assert c in DIGITS[0 ..< radix.int]".