In the tutorial i can read this: Automatic type conversion is performed in expressions where different kinds of integer types are used. However, if the type conversion loses information, the EOutOfRange exception is raised (if the error cannot be detected at compile time).
the question is: what automatic conversion are allowed? Usually, coming from C#, i take this table to understand what is good and it is not: http://msdn.microsoft.com/en-us/library/08h86h00(v=vs.110).aspx
It seems this doesn't work; if i try
var x = 10000'i16 x = x * 100
i get : Error: unhandled exception: over- or underflow [EOverflow] (i noticed this is not EOutOfRange)
that is there is no promotion from int16 to int32. Ok, this is not .Net, so my question is how to avoid this kind of crash in real world programming when data come from sources and ranges are unknown. I could use i64 everywhere but it is a bit unconfortable...
Thx, regards.
contrast:
var x = 10000'i16
var y = 100
echo 'a', x * y # no problem
echo 'b', x * 100'i32 # no problem
echo 'c', x * 100 # EOverflow
So there's automatic conversion from i16 to i32, but 100 is not always interpreted as i32. var x = 10000'i16
import typetraits
echo name(type(x * 100'i32)) # int32: type of expression is the greatest type of two values
echo name(type(x * 100)) # int16, cause 100 has no explicit type
var y: int64 = x * 100'i32 # works, lesser type value (result of the expression)
# is converted to greater type (of the variable)
# x = x * 100'i32 # doesn't work, int32 isn't converted to int16
var y = 100
echo name(type(y)) # int32, cause no type was noticed and it's default integer type
# so (x * y) is the same as (x * 100'i32)
# and (x * 100) is the same as (x * 100'i16)
I don't understand your question "how to avoid this kind of crash in real world programming when data come from sources and ranges are unknown."
If you don't know the range and you want to avoid exceptions, you have two obvious possibilities:
I think that you know that, so what is your "real" question?
May be the question is a bit confused but just for example, i have to work often, in an environment where every single byte of memory is important and where we have to manage many thousand of vars, almost all of them are fully represented via values in a range like 30...128... now, there are exceptions to this behaviour and therefore it would be very handy if we have an automatic conversion form int8 to int16 instead of working with int16 everywhere... Anyway i don't know how Nimrod works representing numbers internally so could not be an issue, i'm still learning the language.
Regards.