11

I have a generic static method that looks like this:

static build<K>() {
    return (GenericClass<K> param) => MyClass<K>(param);
}

So far I have tried:

typedef F = MyClass<K> Function(GenericClass<K> param);

but it says that:

The return type '(GenericClass<K>) → MyClass<K>' isn't a '(GenericClass<dynamic>) → MyClass<dynamic>', as defined by the method 'build'.

and

typedef F = SimpleViewModel<K> Function<k>(Store<K> param);

Which says that:

The return type '(GenericClass<K>) → MyClass<K>' isn't a '<K>(GenericClass<K>) → MyClass<K>', as defined by the method 'build'.

MyClass looks like this:

class MyClass<T> {
  final GenericClass<T> param;

  MyClass(this.param);


  static build<K>() {
      return (GenericClass<K> param) => MyClass<K>(param);
  }
}

So, what is a valid typedef for it?

arielnmz
  • 8,354
  • 9
  • 38
  • 66
  • I might sound stupid but this did it `typedef F = MyClass Function(GenericClass param);` and `static F build() {...}`, makes sense to me, but I can't still explain why, though. – arielnmz Jun 28 '18 at 22:39

1 Answers1

19

There are two concepts of "generic" when it comes to a typedef. The typedef can be generic on a type, or the typedef can refer to a generic function (or both):

A typedef which is generic on T:

typedef F<T> = T Function(T);

Then in usage:

F first = (dynamic arg) => arg; // F means F<dynamic>
F<String> second = (String arg) => arg; // F<String> means both T must be String

A typedef which refers to a generic function on M:

typedef F = M Function<M>(M);

Then in usage:

F first = <M>(M arg) => arg; // The anonymous function is defined as generic
// F<String> -> Illegal, F has no generic argument
// F second = (String arg) => arg -> Illegal, the anonymous function is not generic

Or both:

typedef F<T> = M Function<M>(T,M);

And in usage:

F first = <M>(dynamic arg1, M arg2) => arg2; // F means F<dynamic> so the T must be dynamic
F<String second = <M>(String arg1, M arg2) => arg2; // The T must be String, the function must still be generic on the second arg and return type
Nate Bosch
  • 10,145
  • 2
  • 26
  • 22
  • Can you explain why this doesn't work ? ```typedef F = M Function(T); F first = (String arg){ return arg.length; };``` – Jerome Puttemans Feb 01 '20 at 06:35
  • It looks like you probably want `typedef F = M Function(T); F first = (arg) { return arg.length; };`. In your example you have both types of generics, the tyepdef is generic, and it refers to a generic function. Your concrete function is not generic, it can _only_ return an `int`, it can't be specialized during usage to return a different type. – Nate Bosch Feb 10 '20 at 21:10
  • yes indeed, thank you. The error message is somewhat unclear :-) "The return type 'int' isn't a 'int', as defined by anonymous closure " – Jerome Puttemans Feb 14 '20 at 17:03