Wednesday, September 12, 2012

Change final to const for top-level and static variables now

Kasper Lund, engineer on the Dart project, sent a notice of a new breaking change to the Dart mailing list today.

Kasper writes:


TL;DR: Change final to const for top-level and static variables now.

Over the next days, we will stop treating references to top-level or
static final variables as compile time constant expressions. This
means that the following code will be illegal:

   final X = 42;
   final Y = const [ X ];

because you can only build constant lists out of compile time
constants. The fix is easy. Just replace final with const and you'll
be good to go (this works today):

   const X = 42;
   const Y = const [ X ];

The nice thing is that this paves the way for lazy static
initialization which allow you to initialize top-level and static
(final) variables with non-constant expressions like this:

   final Z = new Set<int>();

Note that this isn't supported across all our platforms just yet.

As always, you can join the discussion at the Dart mailing list, and ask questions at Stack Overflow.