Skip to content

Redefine meaning of "void arrow functions" #33069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks
eernstg opened this issue May 8, 2018 · 2 comments
Closed
2 tasks

Redefine meaning of "void arrow functions" #33069

eernstg opened this issue May 8, 2018 · 2 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-stale Closed as the issue or PR is assumed stale

Comments

@eernstg
Copy link
Member

eernstg commented May 8, 2018

The language team discussed, but did not yet commit to, the following change:

void f() => e; // Static type of `e` can be anything.
// Is syntactic sugar for:
void f() { e; }

g() => e; // Static type of `e` is `void`.
// Is syntactic sugar for:
void g() { e; }

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 type void, 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 type void.

With the old semantics, void f() => 42; would be allowed, but it would be defined to mean void f() { return 42; } which is not allowed. With the adjustment above it would mean void f() { 42; }, which is allowed.

The motivation for the second rule (g) is that it allows the concise notation using => to create a void function, which is in practice what developers want when they write functions like g() => e; where e has static type void.

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:

  • This would probably be directly applicable to library functions and static methods.
  • For instance methods, it might conflict with method signature inference ("inheriting (part of) the signature of a member from overridden declarations"). Presumably, we should still have the semantic effect: void foo() => e; should return null, but method signature inference should have preference (and it could then, e.g., reveal some errors because some methods would have a void returned expression and a non-void return type).
  • This would be applicable to local functions; they already have type inference on the return type, but the fact that the value is discarded (and null is returned) would still make a difference.
  • For function literals we would again rely on the existing inference, but we should have the semantic effect of returning null from () => print('Hello').

Tasks:

@eernstg eernstg added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label May 8, 2018
@lrhn
Copy link
Member

lrhn commented May 8, 2018

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).

@eernstg
Copy link
Member Author

eernstg commented May 9, 2018

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.

@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2025
@lrhn lrhn added the closed-stale Closed as the issue or PR is assumed stale label Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-stale Closed as the issue or PR is assumed stale
Projects
None yet
Development

No branches or pull requests

2 participants