[Proposal] New syntax for lazy getters

Augusto Moura augusto.borgesm at gmail.com
Tue Jun 12 14:24:06 UTC 2018

I don't think a new keyword is the right path here. For classes lazy fields
can easily be implemented via decorators in the last proposal[1] (also,
Groovy has previous work implementing a `@Lazy` annotation[2] with AST
transformations for the same use case). In code something like:
``` js
const lazy = (fieldMetadata) => {
  // change the field value in descriptor to a lazy factory

class Foo {

  @lazy bar = slowCalculation();

  @lazy statements = (() => {
     if (this.bar > 0) {
       return fooify();
     } else {
       return barify();


What it's doesn't cover (and in my opinion should be the focus of a new
proposal) is Decorators for literal objects. Something like the code below
is yet not proposed:

``` js
const foo = {
  @lazy bar: 3,

I didn't made a deep search in ESDiscuss history but I remember this
feature being mentioned sometime ago.

[1]: https://github.com/tc39/proposal-decorators
[2]: http://groovy-lang.org/metaprogramming.html#xform-Lazy
Augusto Moura
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180612/a8978a1e/attachment-0001.html>

More information about the es-discuss mailing list