How do I initialize a static Set field? HashSet seems have no const constructor

1,062 views
Skip to first unread message

Tom

unread,
Jan 20, 2012, 7:28:00 AM1/20/12
to General Dart Discussion
The following code doesn't work:

static final Set<String> foo = const Set.from(const ["a", "b"]);
//An error: 'const' requires const constructor:
'HashSetImplementation.from'
static final Set<String> foo = new Set.from(const ["a", "b"]);
//An error: initializer must be a compile time constant

Any suggestion?

William Hesse

unread,
Jan 20, 2012, 4:35:13 PM1/20/12
to Tom, General Dart Discussion
There is unfortunately no const Set constructor (in the default
implementation of Set). However, there are const Map literals, and a
Map can be used similarly to a Set, so you could use a Map instead:
static final Map foo = const {'a': 'ignore me', 'b': 'ignore me'};
(you might use true instead of 'ignore me'.)

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.

Fei T

unread,
Jan 21, 2012, 12:10:13 AM1/21/12
to General Dart Discussion
Why 'const' must requires const constructor in dart?
In other languages, 'const' could be created by declared as const.

William Hesse

unread,
Jan 21, 2012, 10:41:04 AM1/21/12
to Fei T, General Dart Discussion
What other languages mean by a "const" declaration is a "final"
declaration in Dart. This means that a variable can be set only once,
at the place it is initialized, and can never point to a different
object.

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.

--

Fei T

unread,
Feb 5, 2012, 11:38:47 PM2/5/12
to General Dart Discussion
Thanks for your detailed explanation.
Will dart support run-time constant in the future?

On Jan 21, 11:41 pm, William Hesse <whe...@google.com> wrote:
> What other languages mean by a "const" declaration is a "final"
> declaration in Dart.  This means that a variable can besetonly once,
> at the place it is initialized, and can never point to a different
> object.
>
> 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-finalstaticvariables 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().
>

Gilad Bracha

unread,
Feb 6, 2012, 9:25:44 AM2/6/12
to Fei T, General Dart Discussion
Hi Fei,

On Sun, Feb 5, 2012 at 8:38 PM, Fei T <rave...@gmail.com> wrote:
Thanks for your detailed explanation.
Will dart support run-time constant in the future?


We are looking at liberalizing the rules of static variable initialization.  A detailed proposal should be out later this week. Basically, allow *lazy* initialization of statics to arbitrary run time values.
--
Cheers, Gilad
Reply all
Reply to author
Forward
0 new messages