question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

object destructuring

See original GitHub issue

Using the techniques from #4127 and #4406. It’s now possible to implement destructuring for user-written classes as a syntax sugar over existing constructs in the language. (That is to say, with zero impact upon the backend.)

For example, we could make:

case (Person(name, age)) expr

desugar to:

case (is Person) let ([name, age] = p.destructured) expr

Given a new language module interface Struct shown here.

Or, alternatively, we could make:

case (Person { name, age }) expr

desugar to:

case (is Person) let (name = p.name, age = p.age) expr

Which is a solution that requires no Struct interface or any other kind of “extractor” function, but does not allow easy renaming. (You would have to write Person { fullName=name, age } or whatever if you were fussy about local aliases.)

I have two questions about this:

  1. Which of the above approaches do we prefer? With or without the Struct interface? I lean towards the solution without it.

  2. What does destructuring look like in syntactic locations other than switch? Is it, for example:

    for (Person {name, age} in people) { ... }
    

    Or would it just be:

    for ({name, age} in people) { ... }
    

Thoughts?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:25 (23 by maintainers)

github_iconTop GitHub Comments

4reactions
gavinkingcommented, Mar 28, 2016

For example, I guess you would wind up with something like this:

Person { name, address = { street, city, state, zip } }

Ah no, sorry, my bad. That was silly. You would write it like this:

Person { name, { street, city, state, zip } = address }

or like this:

Person { name, Address { street, city, state, zip } = address }

which is perfectly natural, and totally consistent with the syntax for aliases.

So that’s fine. There’s no problem there for nested patterns.

For literals in patterns I guess you would come up with something like:

Person { name, age == 18 }

which is also OK, I suppose.

3reactions
luconocommented, May 12, 2016

But we also wind up with the most natural way to combine a condition with nested destructuring being:

Person { name, 
         exists address { street, city, state }, 
         is Employment activity { employer, since } }

which looks perfectly reasonable but isn’t the same as the existing syntax for conditions:

Person { name, 
         exists { street, city, state } = address, 
         is Employment { employer, since } = activity }

@gavinking - Agree the first one feels very natural.

I think the following syntax for combining all of destructuring, conditions and aliasing would feel as natural, without being inconsistent with existing syntaxes:

Person { name, 
         exists addressAlias = address { street, city, state }, 
         is Employment activityAlias = activity { employer, since } }

My head naturally parses the { ... } part of address { street, city, state } as simply a destructing operator/operation on address, more than as a local declaration of street, city and state. It just happens that the destructuring has the effect of creating the locals anyway.

So that it really doesn’t feel in conflict with the syntax for conditions:

  • fits the syntax for conditions: (exists localName = expression)
  • also fits the syntax for imports: import some.package { TypeAlias = Type }
  • without the aliases, drops back to exactly what you had in the first one.

Without aliases:

Person { name, 
         exists address { street, city, state }, 
         is Employment activity { employer, since } }

Without conditions (similar to imports):

Person { name, 
         addressAlias = address { street, city, state }, 
         activityAlias = activity { employer, since } }

Without aliases or conditions:

Person { name, 
         address { street, city, state }, 
         activity { employer, since } }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, ...
Read more >
JavaScript Object Destructuring
This tutorial introduces you to the JavaScript object destructuring that assigns properties of an object to individual variables.
Read more >
Destructuring assignment - The Modern JavaScript Tutorial
Destructuring assignment is a special syntax that allows us to “unpack” arrays or objects into a bunch of variables, as sometimes that's more ......
Read more >
How to Use Object Destructuring in JavaScript - Dmitri Pavlutin
The object destructuring is a JavaScript feature to extract properties from objects and bind them to variables.
Read more >
React ES6 Destructuring - W3Schools
We may have an array or object that we are working with, but we only need some of the items ... Destructuring makes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found