Skip to content

Allow redirecting factory constructor to omit parameters #1345

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

Open
a14n opened this issue Dec 2, 2020 · 1 comment
Open

Allow redirecting factory constructor to omit parameters #1345

a14n opened this issue Dec 2, 2020 · 1 comment
Labels
feature Proposed language feature that solves one or more problems

Comments

@a14n
Copy link

a14n commented Dec 2, 2020

When you have redirecting factory constructor you almost always have the same parameters as the target. It would be convenient (and less error prone) to be allowed to omit parameters to specify that the parameters are the same as the target.

//---------today
class A {
  factory A({String? p1, required int p2, required int? p3}) = _A;
}
class _A implements A {
  _A({String? p1, required int p2, required int? p3});
}

//--------- with the proposal
class A {
  factory A = _A;
}
class _A implements A {
  _A({String? p1, required int p2, required int? p3});
}

Moreover this feature would have avoid a bug in the Flutter migration to nullsafety because there was a similar case where the A class was migrated as below:

class A {
  factory A({String p1, required int p2, required int p3}) = _A;  // no question mark added
}
class _A implements A {
  _A({String? p1, required int p2, required int? p3});
}

and this code doesn't trigger any issue unless you have a call like A(p3: null) is your codebase.

@a14n a14n added the feature Proposed language feature that solves one or more problems label Dec 2, 2020
@lrhn
Copy link
Member

lrhn commented Dec 2, 2020

Forwarding parameters with minimal syntax is a recurring request.
The proposal for enhanced default constructors (#698) spends some amount of effort generalizing over forwarding of parameters, but not in a way which then generalizes to other methods (I'm starting to think that's a negative mark against that proposal).
The same thing here ... if it only works for redirecting factory constructors, then it's probably too limited a feature.

I am trying to come up with a way to forward parameters with less syntax, for any method, but it's not trivial.
It's easier to solve for simple cases, like directly calling another function, but then it gets harder again when a function's signature starts depending on the signature of an expression in its body (we try to avoid that kind of dependencies in type inference - so, no-go for foo(*) => test ? bar(*) : baz(*); where * is derived from the signatures of bar and baz.)

(As for the flutter migration, I think a required and nullable parameter is overkill. Make it optional or make it non-nullable. Requiring you to write name: null seems weird. And I actually like that if it had been optional, it would not also have been nullable or exposed an arbitrary default value. That does make forwarding an optional parameter harder, obviously.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

2 participants