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.

thinking about destructuring: allow `value type name` on declarations

See original GitHub issue

What the heck?

Yes, I know, yuck, this breaks the beautiful annotations type/keyword name syntax followed by all declarations. But I think this has already been broken by destructuring.

What do we gain?

Regularity. The language would be easier to learn and easier to compile.

A little bit of backstory

When destructuring was first introduced, I thought it was really irregular that we wrote value foo and String foo while we wrote value [foo] and value [String foo]. This proposal gracefully solves that problem.

Regularity?

Imagine that you want to destructure a tuple. Here is how you’d do it:

value [v] = [""];

Imagine that we’d like to be explicit about the type:

value [String v] = [""];

Here we can clearly see that v is as much of a destructuring pattern as String v.

We can imagine the destructuring syntax being the following:

  • keyword? pattern = rhs

We can imagine the pattern syntax being the following:

  • type? identifier
  • [pattern+]
  • pattern->pattern

is conditions

Whenever the destructuring is done in an is condition, the keyword (is) and all the type must be present. That is, all the following are valid is conditions:

is Foo foo = bar
is [Foo foo] = bar
is Foo foo->Bar bar = baz
is [Foo foo->Bar bar] = baz

While all the following are invalid is conditions:

is foo = bar
[Foo foo] = bar
is Foo foo->bar = baz
is [foo->bar] = baz

exists conditions

Whenever the destructuring is done in an exists condition, the keyword (exists) must be present and each the type may be ommited. That is, all the following are valid exists conditions:

exists Foo foo = bar
exists [Foo foo] = bar
exists foo->Bar bar = baz
exists [foo->bar] = baz

While the following is an invalid exists condition:

Foo foo = bar

for

Whenever the destructuring is done inside a for, the keyword (value) and the each type may be omitted. That is, all the following are valid inside a for:

item in stream
value item in stream
String item in stream
value String item in stream
Integer key->String item  in stream.indexed
value Integer key->item  in stream.indexed

Assignements

Whenever the destructuring is done in a assignment expression, both the keyword and all the types must be omitted. That is, the following are all valid assignment expressions:

foo = bar
[foo, bar] = baz
foo->bar = baz

While the following is an invalid assignment expression:

String foo = bar

Declarations

Whenever the destructuring is done in a declaration, either the keyword (value) must be present, or all the types must be present. In the case the keyword is present, each type may be omitted. That is, the following are all declarations expressions:

value foo = bar
[String foo, String bar] = baz
value [String foo, String bar] = baz

Annotations

Whenever a destructured declaration is annotated with some annotations, it’s the equivalent of each destructured value being annotated with that annotation.

Shortcut refinement

Whenever the destructuring is done in a shortcut refinement declaration, both the keyword and all the types must be omitted. That is, the following are all valid shortcut refinement declarations:

foo = bar
[foo, bar] = baz
foo->bar = baz
Wait. What?

Yes, I know, destructuring for non‐locals, yuck. I’m only adding this for the proposal’s completion sake, because of its regularity. This is a feature you won’t see me defending too much.

Isn’t this more irregular compared to the rest of the language?

Maybe. I think destructuring as it currently exists in the language is pretty irregular by itself, by providing a non‐keyword/type name declaration. One could argue that this, while it makes the destructuring syntax more regular compared with itself, makes it stand out as different and irregular compared to the rest of the language. That’s a reasonable argument in my opinion.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:27 (25 by maintainers)

github_iconTop GitHub Comments

2reactions
gavinkingcommented, May 10, 2017

Thoughts?

@someth2say my thoughts are:

  1. For consistency with assert, if, for, etc, the syntax would be value ([x, y] = [1, 2]);, i.e. the parens would go around the whole thing.
  2. It’s trivial to parse, indeed, it’s somewhat easier to parse than the current syntax, since value ( can’t possibly be the start of a value declaration.
  3. It’s more regular than what we have today, since it’s more similar to the syntax of assert.
  4. I have no objection to it.
1reaction
zamfofexcommented, May 10, 2017

I really don’t understand why you don’t want to discuss this issue, @gavinking. It might not be high priority, but I think it’s important to have something as basic as destructuring declarations be regular.

Additionally, even if you insist it’s “low priority”, it’s not like discussing and trying to solve this problem will cause any sort of harm. Quite the opposite, it might make the language more consistent and overall better.

From the lively arguments the different people are giving, I get the impression that everyone here seems to be interested in continuing to discuss this issue, even if you see it as “low priority” yourself. Even if we disagree on how, it seems to me that everyone is invested in trying to come with a syntax that is better than today’s. I feel like that definitely mean the current syntax is not “perfectly fine”.

Why do you think I spend my time writing these proposals and discussing with people (mainly you) on Gitter? I do so because I care about the language you designed, and I wholeheartedly believe that my proposals can help make it better.

I understand that I’m not the only one collaborating to Ceylon for free, and I know I’m definitely not even close to being the biggest collaborator, but I do what I can do, and what I can do is making suggestions.

I’m really sorry for bringing this up here, but I really feel like I had to say it somewhere — even if I think you won’t want to respond to it here (probably justifiedly).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Breaking down destructuring declarations | Android Developers
Destructuring declarations allows us to define local values/variables as such: It's a convenient way to work with data coming from functions or ...
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 >
Easier destructuring with type annotations on binding patterns
It would allow for type-safe destructuring of values where the type cannot be inferred by the compiler (such as function parameters). Examples.
Read more >
Breaking down destructuring declarations - Kotlin Vocabulary
Sometimes you just want to break down one object that contains multiple fields to initialize several separate variables.
Read more >
[Pitch] Destructuring Assignment of Structs and Classes
Destructuring assignment is a language feature that allows you to extract multiple parts of a value and assign them to multiple variables ...
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