title: How to do lazy evaluation in Dart?
body: <p>Is there a native (language supported) lazy evaluation syntax? Something like <code>lazy val</code> in Scala. </p>
  
  <p>I've gone through <a href="https://www.dartlang.org/docs/dart-up-and-running/ch02.html" rel="noreferrer">the docs</a>, and could not find anything. There is only a chapter about "lazily loading a library", but it's not what I am asking.</p>
  
  <p>Based on this research I incline to believe (please correct me if I'm wrong) that currently there is no such thing. But maybe you know of any plans or feature requests which will provide the functionality? Or maybe it was considered and rejected by the Dart team?</p>
  
  <p>If indeed there is no native support for this, then what is the best practice (best syntax) for implementing lazy evaluation? An example would be appreciated.</p>
  
  <p><strong>Edit:</strong> </p>
  
  <p>The benefits of the feature that I am looking for are mostly the same as in implementation in other languages: <a href="http://www.scala-lang.org/files/archive/spec/2.11/05-classes-and-objects.html#lazy" rel="noreferrer">Scala's <code>lazy val</code></a> or <a href="https://msdn.microsoft.com/en-US/library/dd642331(v=vs.110).aspx" rel="noreferrer">C#'s <code>Lazy&lt;T&gt;</code></a> or <a href="http://docs.hhvm.com/manual/en/hack.attributes.memoize.php" rel="noreferrer">Hack's __Memorize attribute</a>:</p>
  
  <ol>
  <li>concise syntax</li>
  <li>delayed computation until the value is needed</li>
  <li>cache the result (the <em>by-need</em> laziness)</li>
  <li>don't break <em>pure functional</em> paradigm (explanation below)</li>
  </ol>
  
  <p>A simple example:</p>
  
  <pre><code>class Fibonacci {
  
    final int n;
    int _res = null;
  
    int get result {
      if (null == _res) {
        _res = _compute(this.n);
      }
      return _res;
    }
  
    Fibonacci(this.n);
  
    int _compute(n) {
      // ...
    }
  }
  
  main(List&lt;String&gt; args) async {
    print(new Fibonacci(5).result);
    print(new Fibonacci(9).result);
  }
  </code></pre>
  
  <p>The getter is very verbose and has a repetitive code. <strong>Moreover I can't make the constructor <code>const</code></strong> because the caching variable <code>_res</code> has to be computed on demand. I imagine that if I had a Scala-like <code>lazy</code> feature then I would also have language support for having a constant constructor. That's thanks to the fact, that the lazy evaluated <code>_res</code> is <a href="https://stackoverflow.com/questions/8346119/a-grasp-of-immutable-datastructures/8346779#8346779">referentially transparent</a>, and <a href="http://clojure.org/transients" rel="noreferrer">would not be in the way</a>.</p>
  
  <pre><code>class Fibonacci {
  
    final int n;
    int lazy result =&gt; _compute(this.n);
  
    const Fibonacci(this.n);  // notice the `const`
  
    int _compute(n) {
      // ...
    }
  }
  
  main(List&lt;String&gt; args) async {
    // now these makes more sense:
    print(const Fibonacci(5).result);
    print(const Fibonacci(9).result);
  }
  </code></pre>