-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow type instantiation as expression. #123
Comments
We have discussed this feature earlier, along with some grammar ambiguities. I believe that we could avoid those syntactic difficulties by putting this construct near |
We could also consider generic getters as part of this. Unfortunately, I haven't found any good syntax for generic setters. |
I think constructor tear-offs are more useful than Type objects, so I wish we could make those the default. Otherwise, things like this don't work (do they?): (removeDuplicates ? List.from : Set.from)(elements) Otherwise, this SGTM.
You know, I feel like I should want this in principle because it generalizes, but I can't say I've ever actually found myself wanting it in practice.
Eek, yeah. Does this not work: object.setter<T> = value;
setter<T> = value; ? |
It would be breaking to make Since |
This should cover nullable types as well: e.g |
Allowing |
Note that a very old SDK issue, dart-lang/sdk#11923, was used to discuss this topic as well. I closed dart-lang/sdk#11923 as a duplicate of this one, because this one is directly phrased as a language feature proposal and has the more recent discussion. |
Added label 'constructor-tearoffs' — it's a completely different topic, but there is an organizational relation: This feature is actually being implemented now as part of the experiment |
Closing: This feature is included today. |
Dart does not allow you to write
List<int>
as an expression evaluating to aType
object, instantiating the generic class. This is inconvenient to users, who have to resort to helper functions likeType typeOf<T>() => T;
.Dart does not allow you to write
genericFunction<int>
to create a specific type instantiation of a generic function, but it does allowT id<T>(T x) =>x; int Function(int) intId = id;
to implicitly instantiate the generic function. It should be possible to write the type explicitly.Dart does not allow constructor tear-offs - the syntax
Foo<int>
orFoo<int>.named
are not valid expressions. ifFoo<int>
was allowed syntax, then this would be an easy extension.So, as a feature, Dart should allow type instantiation of all generic elements (typedefs, classes and functions) and allow constructor tear-offs.
The syntax
List<int>
can be seen both as a type and as the name of the unnamed constructor. I propose using the context type to distinguish: If the context type isFunction
or any function type, the expression is considered a tear-off of the unnamed constructor, otherwise it is aType
object.This looks like two features, but because of the syntactic overlap, they should be implemented at the same time.
And if
expression < type* >
is valid expression syntax, we can also do explicit instantiation of generic method tear-offs.(If we extend type literal syntax to allow
List<int>
, then we might also want to allowint?
as a NNBD type literal).The text was updated successfully, but these errors were encountered: