I'm a little bit confused. The range of uint32 is 0..4294967295. But it is impossible to assign a variable (uint32) a value larger than 2147483647 in the editor. The Nim project check says instantly: type mismatch: got <int64> but expected 'uint32'. (I'm using VSCodium as editor.) By the other side you can do the following:
var test: uint32 = 2147483647
test = 2 * test + 1
echo test # 4294967295 highest uint32 value
It works like expected. So I think, the editor, especially the code ckeck, is the reason. Can someone give a hint, how to solve this problem?Default literals are of int type. Use
var i: uint32 = 4294967295.uint32
var j: uint32 = 4294967295'u32
echo i
echo j
Sorry to ask again. But on one condition:
Default literals are of int type.
shouldn't the following assignment be incorrect too?
var i: uint32 = 2147483647
i is declared as uint32 and is assigned an integer by default. But this is treated as a correct assignment. Is it checked here whether the assigned value matches the range of the declared type and is therefore 'still' valid?var i: uint32 = 2147483647
echo i
echo typeof(i)
echo typeof(2147483647)
$ nim c -r t.nim
2147483647
uint32
int
So for this case (non negative int) the compiler does an automatic type conversion. It is compile-time conversion, so no runtime costs. If you don't like it, you may tell Araq.