I did some experiments with the idea of making integer literals their own type to make code like myInt16 += 1 compile but decided that it's too complex as -1 or 5+6 should be of the integer literal type too then and thus the type would somewhat depend on the compiler's constant folding capabilities.
Instead I plan to have these integer promotion rules:
This has the effect that the language does not promote i16 + 23 to int anymore; which is convenient for some embedded systems. Of course the same applies to int32, so the pain to work with int32 throughout for cache efficiency should be significantly lessened.
The compiler rejects constant literals that are not in the range of the target type, so 34'i16 + 200_000 will produce a compile-time error ("cannot convert 200_000 to int16"). There will also be warning message emitted for implicit narrowing conversions like myIntVar + i16.
Oh, and I've already implemented these new rules and afaict they don't break any code.
Opinions?
test(4,5) #error, cannot cast int to float automatically
The details are here: http://build.nimrod-code.org/docs/manual.html#pre-defined-integer-types
and here: http://build.nimrod-code.org/docs/manual.html#subrange-types
The change that breaks most code is that int is not converted implicitely to int32 anymore as this can lose information on 64bit systems. Please complain if it breaks too much code; I may add this conversion back then...
Hello,
Is-it possible to define TSubrange = range[0..UINT_MAX]? And what happens when an operation overflow the range?
Thanks.
There seems still some work to do - this does not compile anymore:
var x = 1
x += x and 1
but this does :
inc(x, x and 1)
Is-it possible to define TSubrange = range[0..UINT_MAX]? And what happens when an operation overflow the range?
No, uint and uint64 are no ordinal types for implementation simplicity. The compiler would need to use bignums internally everywhere then and that's too much work for now. However range[0u16 .. high(uint16)] is merely an alias for uint16; you can't get overflow checking for unsigned this way. If you want sane semantics, screw unsigned numbers: http://critical.eschertech.com/2010/04/07/danger-unsigned-types-used-here/
There seems still some work to do ...
Your help is really appreciated. And yeah I noticed many bugs too. I'm working on it. ;-)