NON-BREAKING CHANGE: Lazy initialization has landed!

224 views
Skip to first unread message

Kasper Lund

unread,
Sep 14, 2012, 9:34:31 AM9/14/12
to General Dart Discussion
Dartisans,

We now have one of the major new language features of M1 implemented
across the board. That's right -- you can now use lazy initialization
for top-level and static variables (both final and non-final). This
means that the restriction that used to force the initialization
expression for such a variable to be a compile-time constant is no
more. You can now write:

final worklist = new Queue();
var compiler = new Compiler(worklist);

and both worklist and compiler will be initialized on first access.
We've also lifted the restriction for instance fields so the following
code now runs just fine:

class Cache {
final Map table = new Map();
...
}

as long as the initialization expressions do not refer to [this]. Such
expressions are evaluated when the object is created (not lazily on
first access as we do for static and top-level variables), but it cuts
down on the need for lengthy initializer lists in your constructors.

Cheers,
Kasper

Paul Brauner

unread,
Sep 14, 2012, 9:42:06 AM9/14/12
to General Dart Discussion
Awesome! I can now simplify the API of dart-enumerators.

On Fri, Sep 14, 2012 at 3:34 PM, Kasper Lund <kas...@google.com> wrote:
Dartisans,

We now have one of the major new language features of M1 implemented
across the board. That's right -- you can now use lazy initialization
for top-level and static variables (both final and non-final). This
means that the restriction that used to force the initialization
expression for such a variable to be a compile-time constant is no
more. You can now write:

  final worklist = new Queue();
  var compiler = new Compiler(worklist);


A very random example ;)
 
and both worklist and compiler will be initialized on first access.
We've also lifted the restriction for instance fields so the following
code now runs just fine:

  class Cache {
    final Map table = new Map();
    ...
  }

as long as the initialization expressions do not refer to [this]. Such
expressions are evaluated when the object is created (not lazily on
first access as we do for static and top-level variables), but it cuts
down on the need for lengthy initializer lists in your constructors.

Cheers,
Kasper

--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart



John Evans

unread,
Sep 14, 2012, 11:12:18 AM9/14/12
to mi...@dartlang.org
Huzzah!! So happy about this!  Great work Dart team.

Bob Nystrom

unread,
Sep 14, 2012, 12:49:33 PM9/14/12
to mi...@dartlang.org
On Fri, Sep 14, 2012 at 8:12 AM, John Evans <pru...@gmail.com> wrote:
Huzzah!! So happy about this!  Great work Dart team.

Me too! :D

- bob
 
--
Reply all
Reply to author
Forward
0 new messages