Destructuring and evaluation order

Andreas Rossberg rossberg at google.com
Fri Apr 25 04:24:55 PDT 2014


The way destructuring assignment currently is specified leads to
rather inconsistent evaluation order. Consider:

  let o = {}
  function f() { print(1); return o }
  function g() { print(2); return 5 }

  f().x = g()    // 1, 2
  {a: f().x} = {a: g(}}   // 2, 1

That is, destructuring assignments violate the left-to-right
evaluation that is otherwise maintained.

A similar problem is created by computed property names, and this one
also applies to destructuring binds:

  function h() { print(1); return "a" }
  function k() { print(2); return "a" }

  let {[h()]: x} = {[k()]: 5}  // 2, 1

I'm wondering if this is the behaviour we want. Of course, it is
easier to specify that way (and marginally easier to implement), but
I'm not sure if it is good to violate left-to-right evaluation like
that. In particular, the discrepancy between the two assignments in
the first example is rather irritating, as it violates the spirit of a
regular and composable pattern language.

(Note that either way, left-to-right evaluation order of course can
only hold up to default initializers, but that arguably is natural.)

Thoughts?

/Andreas


More information about the es-discuss mailing list