Skip to content

Specification: Specify that a function literal in an initializer list must be parenthesized #35674

Closed
@eernstg

Description

@eernstg

Cf. #35673, it is currently not specified that the following is an error:

class A {
  final x;
  A(p): x = (p) => 42 {}
}

because the initializing expression in a <fieldInitializer> cannot be a function literal (the problem is that this in some cases introduces the need for an unlimited lookahead, and it's simply confusing for developers because of the syntactic ambiguity of a potentially large amount of code).

We should specify this. It may be possible to relax these rules somewhat, e.g., by noting that (int p) cannot be an initializer, so x = (int p) { return 42; } would not have the ambiguity: We definitely need to parse a function literal, and we can see, after a few tokens, that the {...} is part of that function literal, and then it's required to have a constructor body or a semicolon afterwards. We might also insist that it is an error, because it's still a source of confusion for human readers that we have this big {...} that might be a constructor body.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-specification (deprecated)Deprecated: use area-language and a language- label.closed-duplicateClosed in favor of an existing reporttype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions