There was a previous topic of why positional based destructuring is dangerous but JetBrains Team members refused to acknowledge that there is a better method of destructuring. The previous discussion it seemed that the JetBrains Team got it in their head the the ask was to remove positional destructuring for named destructuring, however, both can exist without issue.
Name based destructuring would be an incredible add to the language. Arguments that were made by the JetBrains Team as to why positional destructuring is going to stay, but there were zero good arguments made as to why named destructuring shouldn’t be added/considered.
@gidds you argument about it seeming overly restrictive by removing any choice about the local variable name does not actually apply to ES as it allows you to remap the properties to new local names, and, in the cases where named destructuring is actually helpful, you are likely just using the property name as the local name anyway, so even if you weren’t able to choose the local name, it is a pretty minor issue. Then there is the “issue” that you bring up with renaming properties, which in my opinion isn’t an issue at all. Compared to positional destructuring, sure, it is more of an issue because renaming properties doesn’t break positional destructuring, but changing order of them breaks positional destructuring worse than renaming breaks named destructuring. Changing the name of a property that is being accessed via named destructuring can and should result in compiler errors (unless you rename or add another property to the same name), but also, because it can be determined explicitly which properties are tied to which destructured local variables, the IDE should be able to rename them all (or if you desire the local names to be kept the same, it can adjust the destructuring to account for that).
Now, as for the syntax, being able to provide both an explicit type and mapped name in a named destructuring declaration is perhaps clunky, but we already have import aliases using as
, which could be the way that the name mapping is done while also still allowing for explicit type declarations.
val (_, _, _, result) = doThing()
could become val { result : Result as localResult } = doThing()
or val { result as localResult } doThing()
it may make more sense for the local name to go first and perhaps use by
instead of as otherwise it could look more like a type cast which it isn’t.
at the very least, it could possibly be supported as a compiler extension.