How to typedef a generic function?

Issue

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?

Solution

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

Answered By – Nate Bosch

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *