Open eernstg opened 6 years ago
Also document how this applies to instance methods with a type variable as return type, when the type variable is instantiated with void
. (It doesn't, the static type is not void
, and the runtime type is Object
anyway).
Yep, rule 1 would apply for a literal void
return type, and there is no change to the semantics of a function whose declared return type is a type variable.
Similarly, rule 2 would only apply when the static type of the returned expression is void
, and the new semantics does not apply when e
has type X
which is a type variable, no matter which value X
is bound to when the given function is executed.
The language team discussed, but did not yet commit to, the following change:
The motivation for the first rule is that it eliminates an inconsistency in the rules:
return 42;
is an error in a{}
function with return typevoid
, based on the consideration that it is too error-prone: It looks like that value is being passed on and used, but that value is actually marked as invalid ("to be discarded") by the return typevoid
.With the old semantics,
void f() => 42;
would be allowed, but it would be defined to meanvoid f() { return 42; }
which is not allowed. With the adjustment above it would meanvoid f() { 42; }
, which is allowed.The motivation for the second rule (
g
) is that it allows the concise notation using=>
to create avoid
function, which is in practice what developers want when they write functions likeg() => e;
wheree
has static typevoid
.We will need a feature specification for this, and the following topics are among the obvious ones that we have not yet considered in detail:
void foo() => e;
should returnnull
, but method signature inference should have preference (and it could then, e.g., reveal some errors because some methods would have avoid
returned expression and a non-void return type).null
is returned) would still make a difference.null
from() => print('Hello')
.Tasks: