simit icon indicating copy to clipboard operation
simit copied to clipboard

Automatic type casting (int->float)

Open fredrikbk opened this issue 8 years ago • 3 comments

Right now Simit is very particular about the type of literals, and does not automatically cast floats to ints. This results in a type error for this code:

a : float = 1;

Obviously we want to assign 1.0 to a, but the type checker reports an error because I am trying to assign an integer to a float.

fredrikbk avatar Aug 05 '16 00:08 fredrikbk

Based on a conversation with @DannyKaufman it might be preferable to keep the current prickly syntax to force programmers to be exact. The casting also raises tricky inference questions. For example, if a vector is inferred to be an int, should it then be auto-casted to float (an expensive operation) as in:

a = [1    2];
b = [1.0, 2.0];
c = a + b  % Is this allowed? What's the type of c? 

One option is to only allow casting of scalars, but then you have to declare the type of float vector literals that are initialized using scalar:

a : vector[2](float) = [1 2];  % Declare type to force scalar type casting

which is not very satisfying.

fredrikbk avatar Aug 12 '16 17:08 fredrikbk

Is the plan then to add support for some set of type conversion intrinsics that programmers can use to do type conversion manually? That'd be needed for something like this for example:

var a : vector[5](float);
for k in 0:5
   a(k) = k + 1;
end

stephenchouca avatar Aug 12 '16 18:08 stephenchouca

Explicit scalar casting

fredrikbk avatar Sep 30 '16 15:09 fredrikbk