_distance
变量是否可能是最终变量,但仍然在Dart对象的构造函数中保留类型签名?
Function _distance;
ExampleConstuctor(num distanceFunction(T a, T b)) {
_distance = distanceFunction;
}
答案 0 :(得分:4)
在1.24.0
中,我们正在引入一种新语法来完成这项工作:
https://github.com/dart-lang/sdk/blob/master/CHANGELOG.md#language-1
typedef F = void Function(); // F is the name for a `void` callback.
int Function(int) f; // A field `f` that contains an int->int function.
class A<T> {
// The parameter `callback` is a function that takes a `T` and returns
// `void`.
void forEach(void Function(T) callback);
}
// The new function type supports generic arguments.
typedef Invoker = T Function<T>(T Function() callback);
在此之前,您还可以使用package:func
中声明的各种typedef:
答案 1 :(得分:2)
您可以使用typedef:
typedef num DistanceFunction<T>(T a, T b);
class ExampleConstructor<T> {
final DistanceFunction<T> _distance;
ExampleConstructor(this._distance);
}
答案 2 :(得分:1)
除上述答案外,我还发现这有效:
class ExampleConstructor<T> {
final Function _distance;
ExampleConstructor(num this._distance(T a, T b))
}