statecharts
statecharts copied to clipboard
Type inference of literals should depend on context
Consider an extended type system where you have, e.g. extended integer types (int16, int32) being all subtypes of integer. Consider following piece of code:
var x : int16
x = 42
The current type inferrer would conclude that variable x is of type int16 and 42 of type integer. Since integer is a supertype of int16, 42 can not be assigned to x. To make this work, we can either provide a custom type validator with some special logic for "assertCompatible". But this feels more like a hack around the problem that 42 is inferred as integer instead of the expected type int16.
My suggestion is to extend the logic of the basic type inferrer to examine the context of literal expressions and return the expected type if compatible, rather than the generic supertype.
Another approach would be to check for all assignments if the right hand can be assigned to the left hand side without losing information. Here that would be possible because an int16
fits a 42
. An uint16
for example would not fit -42
.
This could be done by specifying the lower and upper bounds of types.