Skip to content

[Declaring Constructors] Warning for ambiguous factory method/factory constructor #61910

@kallentu

Description

@kallentu

Context: new syntax changes in dart-lang/language#4485 (comment):

Erik wrote the following -

The ambiguity associated with methods named factory has been resolved in favor of a constructor:

class B.(final int i) {
factory() => B.
(42);
}

The declaration in the body of B declares a factory constructor whose name is B (so you can invoke it using B()), not a method whose name is factory. If you have methods whose name is factory then the recommended way to avoid or fix the breakage is to add a return type (which makes it unambiguously a method).

factory() => ...; will work in both old language versions and the version that we release declaring constructors on, but have two different meanings.

Could we create a lint by this upcoming Dart version to move people away from writing factory()? If it's meant to be a method, they should write a return type, otherwise it'll be parsed as a factory constructor (soon).

cc. @bwilkerson

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.feature-declaring-constructorsImplementation of the declaring constructors feature. Otherwise known as primary constructors.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions