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

[Dot Shorthands][spec] Redirecting factory constructor #4227

Open
kallentu opened this issue Jan 9, 2025 · 4 comments
Open

[Dot Shorthands][spec] Redirecting factory constructor #4227

kallentu opened this issue Jan 9, 2025 · 4 comments
Labels
dot-shorthands Issues related to the dot shorthands feature. feature-completeness A special or edge case of another feature which isn't supported

Comments

@kallentu
Copy link
Member

kallentu commented Jan 9, 2025

This was brought up from some language tests I was working on in this CL.

class Factory {
  final int x;
  Factory.ctor(this.x);

  factory Factory.fct(int i) = .ctor; // (1)

  factory Factory.fctSub(int i) = .ctorSub; // (2)
}

I don't think there's any part of the spec that specifies if we can use enum shorthands as the redirectee in a redirecting factory constructor.

  1. Do we allow (1)?
    a. If so, what is the context type here? The enclosing class?

  2. If we're not allowing subtyping, we probably won't allow (2) right?

We should update the spec to mention whatever we decide in this issue as well.

cc. @dart-lang/language-team

@kallentu kallentu added dot-shorthands Issues related to the dot shorthands feature. feature-completeness A special or edge case of another feature which isn't supported labels Jan 9, 2025
@tatumizer
Copy link

Related to (2):

Object o = .hashAllUnordered; // function is assignable to Object
Object o = .hashAllUnordered([1, 2]); // int is assignable to Object

Is that OK? (Probably yes).

@eernstg
Copy link
Member

eernstg commented Jan 10, 2025

We could say that the context type is the enclosing class, such that the redirectee can be any constructor from the same class. This is not very powerful, but it seems to be the consistent choice.

We could then consider a combination with #4144:

// Current Dart.

class ImagineThatThisIsATerriblyLongName {
  final Object? value; // Actually: 'String | int'.
  ImagineThatThisIsATerriblyLongName._(this.value);
  factory ImagineThatThisIsATerriblyLongName.f1(int value) =
      ImagineThatThisIsATerriblyLongName._;
  factory ImagineThatThisIsATerriblyLongName.f2(String value) =
      ImagineThatThisIsATerriblyLongName._;
}

// With this issue.

class ImagineThatThisIsATerriblyLongName {
  final Object? value; // Actually: 'String | int'.
  ImagineThatThisIsATerriblyLongName._(this.value);
  factory ImagineThatThisIsATerriblyLongName.f1(int value) = ._;
  factory ImagineThatThisIsATerriblyLongName.f2(String value) = ._;
}

// With this issue and #4144

class ImagineThatThisIsATerriblyLongName {
  final Object? value; // Actually: 'String | int'.
  ._(this.value);
  factory .f1(int value) = ._;
  factory .f2(String value) = ._;
}

Why not? ;-)

@lrhn
Copy link
Member

lrhn commented Jan 10, 2025

As specified: No.
The rediction is not an expression, so it's not covered by the specified syntax. We'd have to specify it as a special rule for redirecting factory constructors, like we do for patterns. Then we would have to say which context type to give it (the current class, pretty obviously, since there are no other available alternatives).
It would have to target a constructor, so it's not "any name on the context type". There is no way to add selectors after it.

It's not that common to want a redirecting factory constructor to redirect to the same class. Then you could just call that constructor directly. Redirecting factory constructors are mostly used for calling subclass constructors, which this syntax cannot do.

We can definitely allow it. I don't think it's going to matter much, but if it's not expensive (and it seems pretty simple to just use the same class with the same type arguments as the target), it may be a nice orthogonality.

We could also just allow = this.id(...) for redirecting factory constructors, the same we do for redirecting generative constructors, and then the shorthand is probably moot.

@rrousselGit
Copy link

Its usefulness is probably small enough that I doubt many would care if shorthands didn't apply to redirecting factory constructors.

To begin with, we have the : this() construct for such thing:

class Factory {
  Factory.ctor(int x);

   Factory.fct(int i):  this.ctor(i);
}

@kallentu kallentu changed the title [Enum Shorthands][spec] Redirecting factory constructor [Dot Shorthands][spec] Redirecting factory constructor Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dot-shorthands Issues related to the dot shorthands feature. feature-completeness A special or edge case of another feature which isn't supported
Projects
None yet
Development

No branches or pull requests

5 participants