如何使工厂构造函数返回为const值

时间:2014-10-15 10:55:44

标签: dart

是否可能返回另一个const实现?

abstract class Foo<T> {
  factory Foo(T thing) => const FooImpl(thing); // <= Arguments of a constant creation must be constant expressions
  T get thing;
}

class FooImpl<T> implements Foo<T>{
  final T thing;
  const FooImpl(this.thing);
}

1 个答案:

答案 0 :(得分:4)

Dart有一个委托工厂构造函数来允许这个

abstract class Foo<T> {
  const factory Foo(T thing) = FooImpl;
  T get thing;
}

class FooImpl<T> implements Foo<T>{
  final T thing;
  const FooImpl(this.thing);
}

另见

https://groups.google.com/a/dartlang.org/forum/#!topic/misc/cvjjgrwIHbUhttps://groups.google.com/a/dartlang.org/forum/#!topic/misc/cvjjgrwIHbU