Skip to content

Analyzer comment indicates a factory constructor can have a return type #60563

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
srawlins opened this issue Apr 18, 2025 · 1 comment
Closed
Assignees
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. type-documentation A request to add or improve documentation

Comments

@srawlins
Copy link
Member

The comment on ConstructorDeclaration.redirectedConstructor (public API) says:

The type of object being created.

This can be different than the type in which the constructor is being
declared if the constructor is the implementation of a factory
constructor.

I'm not sure if that was ever true, but it isn't true now. (It'd be a cool feature.)

This code:

class C {
  factory D C.foo() {
    return D();
  }
}

class D implements C {}

results in several errors, among them:

The name of a factory constructor must be the same as the name of the immediately enclosing class.

And this code:

class C {
  D factory C.foo() {
    return D();
  }
}

class D implements C {}

results in one error:

Factory constructors cannot have a return type.

Looking at the call sites of ConstructorDeclarationImpl, the value of returnType is always a SimpleIdentifier for the type name, in either a named or unnamed constructor.

@srawlins srawlins added area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. type-documentation A request to add or improve documentation labels Apr 18, 2025
@srawlins srawlins self-assigned this Apr 18, 2025
@FMorschel
Copy link
Contributor

We have this for the discussion you suggested:

I guess the comment you mentioned was talking about the factory constructor being able to return subclasses but the wording was unfortunate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. type-documentation A request to add or improve documentation
Projects
None yet
Development

No branches or pull requests

2 participants