Saturday, March 24, 2012

Plan for Laziness in Dart

‹prev | My Chain | next›

With exactly one week to go until the release of the first edition of Dart for Hipsters, the Dart team released a new version of the language specification. To this, I can only say, guys, seriously, we definitely need better coordination for the second edition. I kid!

Reading through the spec, I do not see much that requires changes to the beta version of the book. To be sure, method cascades would require a new section, possibly even a new chapter, but they are not yet baked, let alone implemented.

One thing that does bear investigation is lazy evaluation of static variables. I have been bit by "must be a compile time constant" enough to see it in my sleep. Yes, my dreams are just that boring—but (hopefully) no more!

For example, in Hipster MVC's pushState manager, HipsterHistory, I cannot simply initialize the routes class variable like so:
class HipsterHistory {
  static List routes = [];
  // ...
The empty array to which I am attempting to assign routes is not a compile time constant, so I would get a compile time error.

Instead, I have needed to make a pretend routes static variable via a getter. That getter can initialize a private _routes class variable if needed and return the current value:
class HipsterHistory {
  static List _routes;

  static get routes() {
    if (_routes == null) _routes = [];
    return _routes;
  // ...
That, of course, it is a silly amount of work to go through for what ought to be a one-liner. Hence the updated spec. I download the latest copy of Dartium and give the simpler version a go. Sadly, I still get the same error:

Ah well, at least I know that help is on the way. I think for first edition purposes, I will likely describe lazy evaluation as the One True Way™, but with an aside briefly describing the old behavior.

Day #235

No comments:

Post a Comment