-
Notifications
You must be signed in to change notification settings - Fork 207
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
Comments
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). |
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? ;-) |
As specified: No. 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 |
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 class Factory {
Factory.ctor(int x);
Factory.fct(int i): this.ctor(i);
} |
This was brought up from some language tests I was working on in this CL.
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.
Do we allow (1)?
a. If so, what is the context type here? The enclosing class?
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
The text was updated successfully, but these errors were encountered: