[RFC] Type of `return` is wrong
supercooldave opened this issue · comments
The type rule (and corresponding implementation) of the type of return
is as follows:
E |- expr : t
E |- currentMethod : _ -> t
-----------------------------
E |- return expr : t
I argue that this is wrong, and the type should be unit
or, even better, anything.
E |- expr : t
E |- currentMethod : _ -> t
-----------------------------
E |- return expr : t'
Consider the following example (out of context):
if foo then
return 10
else
this.skip()
end
where this.skip()
has type unit
.
This does not type check, even though the only one of the branches actually returns a result.
I would argue that making the type anything (which is probably Bottom
in the current implementation) rather than unit
will allow for more flexibility, permitting the following example to compile:
def bar() : String
"bar"
end
def foo() : Maybe[int]
if true then
return Nothing : Maybe[int]
else
this.bar()
end
Just(11)
end
The fix should be easy, but I'm not sure what the consequence will be. My understanding is that Bottom type does not play nicely.
Similar comments apply to forward
.