Proposal: Nominal deconstruction #3546
Replies: 5 comments
-
It's probably worth pointing out that since Javascript introduced the ability to deconstruct like that it became incredibly popular, to the point that some devs seem to rarely use dot-syntax for properties. Here's some code from one of our repos committed not too long ago, and this pattern is present everywhere: render() {
const {
product: {
id,
name,
url,
sizes,
boutiqueUrl,
imageId,
boutiqueName,
currentPrice,
previousPrice,
freeUkShipping
},
currency,
wrapperClass,
analyticsValuesForPDP,
onProductClick,
loading
}: Props = this.props;
//...
} FWIW It could be that Javascript's dynamic nature is what makes this considered best practice; i.e. the properties that are actually used are all declared up front in the method. |
Beta Was this translation helpful? Give feedback.
-
@Richiban So does javascript use the same identifier name for the property and the local? I think that wouldn't work to well in C#, especially as the naming conventions are different. However the compactness is very nice. |
Beta Was this translation helpful? Give feedback.
-
You get that by default (the local gets the same name as the property if you don't override the name) at the site of deconstruction. In other words: const { x, y } = data; is the same as: const { x: x, y: y } = data; It's identical to C#'s syntax for prop deconstruction in pattern matching, except that C# requires the Typescript const { x: localX, y: localY } = data; vs (theoretical syntax) C#: var { X: var localX, Y: var localY } = data; |
Beta Was this translation helpful? Give feedback.
-
Relevant: #3107 |
Beta Was this translation helpful? Give feedback.
-
just complementing: The advantage of this feature over
|
Beta Was this translation helpful? Give feedback.
-
One very nice feature of recursive patterns is their ability to deconstruct objects into the relevant properties: For example:
I find this much nicer than deconstructing manually:
However this only works when you do a pattern match. In this case
textRange
can be null so a pattern match is necessary, so this works well.What if
textRange
wasn't null? You'd have to keep the pattern match and the condition in order to enable this deconstruction, even though the condition is guaranteed to succeed. This is not very good style.An alternative is to do this:
This is a little bit of a hack, and only works because
TextRange
is a struct, so cannot be null, so the variables are definitely assigned. What ifTextRange
was a class:It would be useful to have a way to explicitly deconstruct something without doing pattern matching.
This can be considered Nominal Deconstruction as a counterpart to the existing Positional Deconstruction. This fits in with the parallel between Positional Records which can be positionally deconstructed, and Nominal Records which must be nominally deconstructed.
Example
Syntax
Semantics
A nominal deconstruction is equivalent to:
Except for:
var
before a nominal_deconstruction, in which case all of the nominal_deconstruction_parts are declarations rather than assignments, and cannot declare their type.Open questions:
Should you be able to assign the deconstructed object to a variable at the same time:
e.g.
Beta Was this translation helpful? Give feedback.
All reactions