On Sat, Jun 9, 2012 at 12:25 AM, Florian Loitsch <
floi...@google.com> wrote:
> On Fri, Jun 1, 2012 at 12:16 PM, Lasse R.H. Nielsen <
l...@chromium.org>
> wrote:
>> Yes, you have to declare val as const as well. I believe that's
>> allowed by the specification.
>> Then you can write:
>> switch (test.val) {
>> case MyEnum.First.val: ...
>
> A little late to the party, but if I'm not wrong reading a field from a
> compile-time constant is not a compile-time expression.
As I read it, if the field is declared const, it should be a constant
variable (
http://www.dartlang.org/docs/spec/latest/dart-language-specification.html#kix.6b1cgvgf1cyq)
and a reference to a constant variable is a compile-time constant
expression (
http://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.9asvt38phduq).
The big questions is what "reference to a constant variable" covers.
Is it only local variables, only static variables, or can it also be
instance variables of compile time constant objects?
I can see a good use of the last case (e.g., here), so I obviously
want it to cover that.
On the other hand, it's not at all obvious what:
class A {
const int x = 42;
foo() => const B(x);
}
will do if you subclass A and overwrite x.
You also need different subclasses of the typesafe enum to have
different const variable values, since you can't write:
class E {
const int id;
const E(
this.id);
}
(and that's probably a good thing too).
My guess is that const instance variables will be disallowed
eventually, but the current spec allows them, and may or may not allow
references to them through compile time constant instances.
Ofcourse const declarations are not implemented anywhere yet. :)