Why can't I use generic Type as an argument in Dart 2?

Viewed 136

This seems to be allowed

  List<Type> myTypes = List();
  myTypes.add(SomeType);

  List<Type> moreTypes = [int, String];

but this fails?

  List<Type> myTypes = List();
  myTypes.add(SomeGenericType<int>);

  List<Type> moreTypes = [SomeGenericType<int>, SomeGenericType<String>];

The error message is:

The operator '<' isn't defined for the class 'Type'. Try defining the operator '<'.

I'm new to Dart coming mainly from a C,C# background, so this seems confusing and inconsistent. Why is a generic type treated any differently than a non-generic?

1 Answers

This relates to this question: How to get generic Type? - but it isn't a real duplicate question, that's why I replicate it here.

While it doesn't answer the "Why", it gives the solution for the "How":

Helper function:

Type typeOf<T>() => T;

And then you can get the type without a needed instance:

myTypes.add(typeOf<SomeGenericType<int>>());
Related