[Proposal] New syntax for lazy getters

Aadit M Shah aaditmshah at fastmail.fm
Tue Jun 12 07:13:29 UTC 2018

Hello TC39,

I recently opened an issue[1] in the tc39/ecma262[2] repository,
proposing a new syntax for lazy getters, and I was directed to the
CONTRIBUTING[3] page which stated that I should start a conversation on
this mailing list.
So, my feature proposal is to have syntactic sugar for creating lazy
getters[4]. To summarize my original proposal (which you can read by
following the very first link above), I find that creating lazy getters
is very verbose. For example, consider:
const take = (n, xs) => n ===  ? null : xs && {
    head: xs.head,
    get tail() {
        delete this.tail;
        return this.tail = take(n - 1, xs.tail);

My proposed solution is to add a new keyword lazy to the language. This
keyword can only be used as a prefix to longhand property names in
object initializers, and it defers the execution of the value expression
until the property is accessed. In short, it's just syntactic sugar for
lazy getters:
const take = (n, xs) => n ===  ? null : xs && {
    head: xs.head,
    lazy tail: take(n - 1, xs.tail)

This is purely syntactic sugar. The semantics of this new syntax would
remain the same as that of the desugared syntax. In particular, calling
Object.getOwnPropertyDescriptor(list, "tail") would return an accessor
descriptor before accessing list.tail and a data descriptor afterwards.
Furthermore, there are other advantages of having this syntactic sugar.
For example, creating cyclic data structures becomes much easier.
Examples are provided in my original proposal which is linked above.
Hope to hear your thoughts on this.
Aadit M Shah


  1. https://github.com/tc39/ecma262/issues/1223
  2. https://github.com/tc39/ecma262
  3. https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md
  4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get#Smart_self-overwriting_lazy_getters
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180612/788d8e77/attachment-0001.html>

More information about the es-discuss mailing list