Skip to content
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

Compile-time error when using assignment initializer with anonymous functions. #865

Open
hcbpassos opened this issue Mar 3, 2020 · 2 comments

Comments

@hcbpassos
Copy link

hcbpassos commented Mar 3, 2020

Seems like Dart analysis gets confused when using assignment initializer with anonymous functions. For instance:

class Foo {
  final VoidCallback bar;
  Foo() : bar = () {}; // Expected an identifier.
}

When wrapping the function in parenthesis, the error goes away:

class Foo {
  final VoidCallback bar;
  Foo() : bar = (() {}); // Fine.
}

I'm not sure whether this behavior is expected, but I cannot see any reason why it should be.

@lrhn
Copy link
Member

lrhn commented Mar 3, 2020

The problem here is that the parser has a hard time distinguishing function bodies in the initializer list from a constructor body.

It's not impossible, but it's hard enough that Dart has so far not allowed unparenthesized function-expressions in initializer lists.

The example here is easier because there is no argument, but

 Foo(x) : bar = (x) { print(x); };

requires arbitrary look-ahead to see whether the {print(x)} is a function body or a constructor body. (It's probably a function body because of the trailing ;).

I'm not sure the grammar reflects this restriction. @eernstg?

@eernstg
Copy link
Member

eernstg commented Mar 3, 2020

This restriction has been part of Dart since 2013 (cf. dart-lang/sdk#11509), but the language specification does not mention it, and never did. This PR finally adds it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants