-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Destructuring supported? #77
Comments
you could still do |
@ljharb: Well, yes, I believe they should be. It's just not in the current proposal, and should be either added, or explicitly specified that it is not supported. (not sure about that |
it should be the same with or without the export. |
In the case of using I can think in 2 options here that is throwing TypeError or coercing types. However, I don’t like the idea of throwing a TypeError, because it means that we would have some inflexibility when using destructuring into formal parameters. I also don’t like the idea of having type coercion here, because they may lead to a lot of confusion. |
@caiolima if I remember correctly, destructuring is specified as a series of steps being done to set variable values (which is why a value can dynamically use the value of a previous variable in the destructuring). In this case, I see no reason why the right side cannot be an object and the left a record, and vice versa. |
I want to propose that destructuring with record and tuple syntax be a syntax error (at least in this initial proposal). We don't allow most kinds of literals on the |
The right hand side of destructuring is an expression that’s allowed to be any non-nullish object; it seems exceedingly strange to allow disallow record/tuple primitives. Do you mean the left hand side? It makes no sense to me to use record/tuple syntax on the left hand side, since that’s object-like syntax for creating variables, not for creating an object (and thus, not for creating records/tuples). |
@ljharb Heh, yes, edited my comment to refer to the left, not right, side. Is it accurate to say that our proposals (of banning this construct) match? |
Yes, it is - i don’t even see how it could possibly make any sense to allow it. |
Sounds good, we should write down it's not possible to destructure them |
Destructuring is really nice though — instead of deferring the whole thing to a future proposal, could we just disallow the rest operator? const rec = #{ a: "a", nestedRec: #{ deepA: "deepA" } };
const { a, nestedRec } = rec;
assert(a === "a"); // all good
assert(nestedRec === #{ deepA: "deepA" }); // all good
const { a, ...rest } = rec; // runtime error When destructuring an object, you just get back whatever's inside. In this case, what's inside is (and can only be) a primitive or another record/tuple, so you'd get back those. So there's no controversy here, right? Just with the |
@dcporter I agree that we should permit the first part of your sample, but I don't see why we should prohibit the second part. Can't we just say that that's an Object? It's always possible to spread it out into a Record later if that's desired (even as this construct is a bit awkward). |
Hmm — splitting up a record and getting an object felt wrong. But now I'm poking around in the console and I see that I can use Given that, I think it does make sense to allow the * Object-destructuring an array: const arr = [0, 1, 2];
const { 1: one, ...rest } = arr;
console.log(one); // 1
console.log(rest); // { 0: 0, 2: 2 } |
You’re right that a rest arg produces a container value; it does seem reasonable to me not necessarily that you can determine what you get on the LHS with syntax, but that perhaps a rest arg from a Record/Tuple is a Record/Tuple automatically? |
In your example however, to contradict my last comment; note that |
@ljharb right: I’m now saying that destructuring with {} on the |
(gyaaaargh left not right!) |
Note that you can do this all with strings as well: > const { length, ...props } = "hello"
> length
5
> props
Object(5) [ "h", "e", "l", "l", "o" ] // How Firefox console displays it I think the semantics of destructuring point to a clear answer by analogy here, that we just blindly create an Object rather than a Record in this kind of case. |
I agree. (PS I also meant "fiddly" not "cuddly" in my previous; there's probably no less cuddly character than @ljharb you gave my last both a 👍 and a 👎, should I assume that you're in favor of the first half and opposed to |
@dcporter precisely :-D |
I guess in most cases programmers want record/tuple when destructuring record/tuple. So: const [a, ...rest] = #[1, 2, 3]
rest === #[2, 3] // expect true, actual false
// Programmers need to convert it to record/tuple manually
const restTuple = #[...rest] There are several options we could consider:
const [a, ...rest] = #[1, 2, 3]
rest === #[2, 3] // true Pros: Match programmer's expectation in most cases (I suppose)
const [a, #...rest] = #[1, 2, 3] Pros: new syntax never affect old code |
The spread operator on LHS has a lot of real-world good use cases like below.
If you guys think it can break old code where destructuring is always an object then adding a new syntax like below will still be very useful for developers.
|
a later proposal stage)
the semantics. (we can add things in follow-on proposals)
in-the-weeds.
Not sure about this, but is destructuring supported? E.g. does this work:
I'm guessing it would, but it's definitely grey area, and I believe should be mentioned in the proposal.
But it gets worse:
I would probably want
z
to be#{a: 6, b: 7}
and not{a: 6, b: 7}
, so theoretically, we should have this syntax:Sidenote:
If the last is true, it puts a damper on the
const
alternative to#
, because this would look like this:The text was updated successfully, but these errors were encountered: