How to typedef a generic function?
Asked Answered
C

1

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?

Captious answered 28/6, 2018 at 22:33 Comment(1)
I might sound stupid but this did it typedef F<I> = MyClass<I> Function(GenericClass<I> param); and static F<K> build<K>() {...}, makes sense to me, but I can't still explain why, though.Captious
J
21

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
Jard answered 28/6, 2018 at 23:37 Comment(3)
Can you explain why this doesn't work ? typedef F<T> = M Function<M>(T); F<String> first = <int>(String arg){ return arg.length; };Ladew
It looks like you probably want typedef F<T, M> = M Function(T); F<String, int> 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.Jard
yes indeed, thank you. The error message is somewhat unclear :-) "The return type 'int' isn't a 'int', as defined by anonymous closure "Ladew

© 2022 - 2024 — McMap. All rights reserved.