如何使一个工厂构造函数返回作为const值 [英] How to make a factory constructor that returns as const value
本文介绍了如何使一个工厂构造函数返回作为const值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可能返回一个不同的const实现?
abstract class Foo< T> {
factory Foo(T thing)=> const FooImpl(thing); //< =常量创建的参数必须是常量表达式
T get thing;
}
class FooImpl< T>实现Foo T {
final T thing;
const FooImpl(this.thing); Dart有一个委托工厂构造函数,它包含了一个代理工厂构造函数允许此 抽象类Foo< T> {
const factory Foo(T thing)= FooImpl;
T get thing;
}
class FooImpl< T>实现Foo T {
final T thing;
const FooImpl(this.thing);
}
另请参阅
https://groups.google.com/a/ dartlang.org/forum/#!topic/misc/cvjjgrwIHbU 和
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/cvjjgrwIHbU
Is it possibly to return a different implementation of as 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);
}
解决方案 Dart has a delegating factory constructor to allow this
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);
}
see also
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/cvjjgrwIHbU and
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/cvjjgrwIHbU
这篇关于如何使一个工厂构造函数返回作为const值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文