Skip to content

Allow type instantiation as expression. #123

@lrhn

Description

@lrhn

Dart does not allow you to write List<int> as an expression evaluating to a Type object, instantiating the generic class. This is inconvenient to users, who have to resort to helper functions like Type 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 allow T 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> or Foo<int>.named are not valid expressions. if Foo<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 is Function or any function type, the expression is considered a tear-off of the unnamed constructor, otherwise it is a Type 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 allow int? as a NNBD type literal).

Metadata

Metadata

Assignees

No one assigned

    Labels

    constructor-tearoffsfeatureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions