如何typedef一个泛型函数?
How to typedef a generic function?
我有一个通用静态方法,如下所示:
static build<K>() {
return (GenericClass<K> param) => MyClass<K>(param);
}
到目前为止我已经尝试过:
typedef F = MyClass<K> Function(GenericClass<K> param);
但上面写着:
The return type '(GenericClass<K>) → MyClass<K>' isn't a '(GenericClass<dynamic>) → MyClass<dynamic>', as defined by the method 'build'.
和
typedef F = SimpleViewModel<K> Function<k>(Store<K> param);
也就是说:
The return type '(GenericClass<K>) → MyClass<K>' isn't a '<K>(GenericClass<K>) → MyClass<K>', as defined by the method 'build'.
MyClass
看起来像这样:
class MyClass<T> {
final GenericClass<T> param;
MyClass(this.param);
static build<K>() {
return (GenericClass<K> param) => MyClass<K>(param);
}
}
那么,什么是有效的 typedef
?
当涉及到 typedef 时,"generic" 有两个概念。 typedef 可以是类型上的泛型,或者 typedef 可以引用泛型函数(或两者):
在 T
上通用的 typedef:
typedef F<T> = T Function(T);
然后在使用中:
F first = (dynamic arg) => arg; // F means F<dynamic>
F<String> second = (String arg) => arg; // F<String> means both T must be String
引用 M
上的通用函数的 typedef:
typedef F = M Function<M>(M);
然后在使用中:
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
或两者兼而有之:
typedef F<T> = M Function<M>(T,M);
在使用中:
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
我有一个通用静态方法,如下所示:
static build<K>() {
return (GenericClass<K> param) => MyClass<K>(param);
}
到目前为止我已经尝试过:
typedef F = MyClass<K> Function(GenericClass<K> param);
但上面写着:
The return type '(GenericClass<K>) → MyClass<K>' isn't a '(GenericClass<dynamic>) → MyClass<dynamic>', as defined by the method 'build'.
和
typedef F = SimpleViewModel<K> Function<k>(Store<K> param);
也就是说:
The return type '(GenericClass<K>) → MyClass<K>' isn't a '<K>(GenericClass<K>) → MyClass<K>', as defined by the method 'build'.
MyClass
看起来像这样:
class MyClass<T> {
final GenericClass<T> param;
MyClass(this.param);
static build<K>() {
return (GenericClass<K> param) => MyClass<K>(param);
}
}
那么,什么是有效的 typedef
?
当涉及到 typedef 时,"generic" 有两个概念。 typedef 可以是类型上的泛型,或者 typedef 可以引用泛型函数(或两者):
在 T
上通用的 typedef:
typedef F<T> = T Function(T);
然后在使用中:
F first = (dynamic arg) => arg; // F means F<dynamic>
F<String> second = (String arg) => arg; // F<String> means both T must be String
引用 M
上的通用函数的 typedef:
typedef F = M Function<M>(M);
然后在使用中:
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
或两者兼而有之:
typedef F<T> = M Function<M>(T,M);
在使用中:
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