2

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);
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567

1 Answers1

4

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

lrn
  • 64,680
  • 7
  • 105
  • 121
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567