HEADS-project / training

Training material to get started with the HEADS technologies
10 stars 16 forks source link

Allowing implicit type conversions can have unintended problems depending on generated language #94

Closed magnustellu closed 8 years ago

magnustellu commented 8 years ago

Pseudo-code

property na : Integer = 2
property nb : Integer = 3 

property ra : String

function plus(left : Integer, right : Integer) : Integer
do
    return left + right
end

function isFiveAsString(maybeFive : String) : Integer
do
    return maybeFive == "5"
end

ra = plus(na, nb)

print isFiveAsString(ra)

The example looks a bit constructed, but I ran into this issue while doing some work. Right now ThingML allows implicit conversion of types, which works great for numbers (or could possibly be problematic if mixing floating points and integers (3 is not always 3)). In this case I have a function (isFiveAsString) which accepts a string as an argument, checks it against a string ("5"), but it will return false. The ra property, although declared as a string, is a number, since it was created as an addition between to numbers.

I'm not sure if allowing implicit type conversion is a good idea in the long run. Maybe it is something at the very least should be possible to disallow when defining a type?

brice-morin commented 8 years ago

We now have a (simple) type checker that should prevent most of those cases.