如何为现有类型赋予新名称?
假设我想给一个List<String>
名字Directives
,然后可以说directives = new Directives()
。
现在可以通过一个空的 mixin
mixin ToAlias{}
class Stuff<T> {
T value;
Stuff(this.value);
}
class StuffInt = Stuff<int> with ToAlias;
main() {
var stuffInt = StuffInt(3);
}
看起来未发布的 2.13 将包含正确的类型别名,而不需要 mixin。
typedef IntList = List<int>;
IntList il = [1,2,3];
https://github.com/dart-lang/language/issues/65#issuecomment-809900008 https://medium.com/dartlang/announcing-dart-2-12-499a6e689c87
您不能定义别名。对于您的情况,您可以使用quiver 中的 DelegatingList来定义Directives
:
import 'package:quiver/collection.dart';
class Directives extends DelegatingList<String> {
final List<String> delegate = [];
}