You could also make your own class, ConstSet, which implements Set by
wrapping a const Map with the methods of Set:
class ConstSet implements Set {
static final Map map;
const ConstSet(Map this.map);
... define methods...
}
static final ConstSet foo = const ConstSet(const {'a': 'ignore me',
'b': 'ignore me'};);
Hope this helps (and is correct) :)
--
William Hesse
Software Engineer
whe...@google.com
Google Denmark ApS
Frederiksborggade 20B, 1 sal
1360 København K
Denmark
CVR nr. 28 86 69 84
If you received this communication by mistake, please don't forward it
to anyone else (it may contain confidential or privileged
information), please erase all copies of it, including all
attachments, and please let the sender know it went to the wrong
person. Thanks.
The "const" keyword in Dart refers to compile-time constants, and
means an object that can be completely constructed at compile time,
with none of its data depending on run-time information. Static
variables and top-level variables must be initialized with a constant
object. The only variables that can be initialized with a
non-constant object are local variables, and member variables
initialized in a constructor. (The rules seem to say that non-final
static variables of a class can have non-constant initializers, but I
don't see how that could make sense). This is to improve startup
performance, so no dart code is called as part of library
initialization.
The third concept is immutability, whether an object can be changed,
once it is constructed. A final variable can contain (refer to) a
mutable object. Many basic objects, like numbers and strings, are
immutable. It seems like an object created by a const constructor
must be immutable. This is implied by the rule of "canonicalization
of constant list and map literals" (10.6 and 10.7), and that a class
with a constant constructor must have only final instance variables.
Since the constant constructor initialized those final instance
variables to constant objects, they must be immutable as well.
This means, for example, that
const []
is an empty list that will always remain the empty list, and
[]
is an empty list that can have elements added to it with .add().
On Sat, Jan 21, 2012 at 6:10 AM, Fei T <rave...@gmail.com> wrote:
> Why 'const' must requires const constructor in dart?
> In other languages, 'const' could be created by declared as const.
--
Thanks for your detailed explanation.
Will dart support run-time constant in the future?