-
Notifications
You must be signed in to change notification settings - Fork 62
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
let
binding record and tuple should be writable
#372
Comments
Hi @magic-akari Unless I have misunderstood the issue, this sounds like you might have mixed up two unrelated concepts in JavaScript.
Records and Tuples are new types of value, their semantics are encoded internally to the value. If they are accessed via |
Here's an example of how strings (a primitive list vaguely similar to Tuples) currently behave with const isPrimitive = (v) => Object(v) !== v;
assert(isPrimitive("strings"));
const a = "test";
let b = a;
assert(a === b);
b[0] = "T"; // TypeError strings are immutable |
Adding to that, if you request this because deep updates seem complicated to you, please take a look at the deep spread syntax for Records proposal. |
Thanks for your reply. |
As @acutmore mentions, this really would be a change to the semantics of the language that is not specific to records and tuples. Strings are also composite primitive values (a list of codepoints), and if we allow updating individual entries in a tuple the way suggested, there is no reason updating a string shouldn't work similarly. That is a much deeper change to the language which basically boils down to saying that an assignment to a reference may in some case not mutate the value behind the reference, but the value of the base part of the reference. The main problem I see is that there is no way to differentiate from just the reference which case it is, as it depends on the type of the value behind the reference. Having an explicit way of doing this kind of deep assignment seem more appropriate. |
It seems Swift do not use explicit way but not cause confusion. So maybe it's not a big deal. Of coz, Swift is designed to have both value types (structs) and reference types (classes) from the start, JS has different history, we might be conservative on such change. Anyway, I think having an explicit way is a good tradeoff. Possible explicit syntax would be: const foo1 = #{ bar: #{ baz: 123 } };
let foo2 = foo1;
foo2|.bar.baz = 456;
assert(foo1.bar.baz === 123);
assert(foo1 !=== foo2); (Copy @mhofman 's idea from tc39/proposal-deep-path-properties-for-record#19 (comment), but I change |
As a JavaScript developer, I expect to see arrays and objects like primitive value, rather than ones that are completely not writable.
I expect modifying a partial value in tuple/record should update the whole value of tuple/record.
code
The text was updated successfully, but these errors were encountered: