19

如何为现有类型赋予新名称?

假设我想给一个List<String>名字Directives,然后可以说directives = new Directives()

4

2 回答 2

26

现在可以通过一个空的 mixin

mixin ToAlias{}

class Stuff<T> {
  T value;
  Stuff(this.value);
}

class StuffInt = Stuff<int> with ToAlias;

main() {
  var stuffInt = StuffInt(3);
}

编辑 2.13

看起来未发布的 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

于 2018-11-22T11:04:23.843 回答
11

您不能定义别名。对于您的情况,您可以使用quiver 中的 DelegatingList来定义Directives

import 'package:quiver/collection.dart';

class Directives extends DelegatingList<String> {
  final List<String> delegate = [];
}
于 2013-11-03T19:57:17.503 回答