-
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
with syntax #1
Comments
I'm also wondering, how did you want the As a separate issue, if the syntax |
About prototype chain changes: I don't understand why you wouldn't just put the method on the instance. And I don't understand the semantics either: |
I agree with you. I’d like to put the dot. Phil had some strong opinions against it |
TIL, I will probably make a lot of those mistakes looking forward, thank you for looking out for those issues |
+1 for I'm not sure how I feel about the how about:
you end up specifying |
Interesting... I'd say we might need a bit more external input here but otherwise this is a completely reasonable thing to consider |
Shouldn't this feature be made as a separate proposal, since it is orthogonal to the const/immutable value types feature ? It would be (or should be ) applicable to normal mutable value types too, wouldn't it? I see huge value in the const/immutable value types, but I am unsure of the value of the Without the Example: Rather than this: const map1 = const {
a: 1,
b: 2,
c: 3,
};
const map2 = map1 with .b = 5; ... we can do this: const map1 = const {
a: 1,
b: 2,
c: 3,
};
const map2 = const {...map1, b: 5}; I think object spread way would be preferred because it's the familiar way of doing things, and in the above example it's every bit as elegant as the Updating nested values with object/array spread may be slightly less elegant, but I don't think the comparative elegance of using Anyways, shouldn't that be debated and worked on in a different proposal? |
So it's implicit and I should definitely clarify that but the spread syntax is supposed to have the effect you're describing so I'm probably going to steal your example there! Also I think I'm gonna start phrasing the proposal so the core of the proposal is highlighted and we can have additional nice to have points, I think the |
In my experience immutable programming often involves deep paths, and it would be nice to optimize this experience. By optimize I mean two things:
For example, if { ...object, [key1, key2, key3]: value } Then it would be easy to do, and more importantly, allows you to easily make the keyPath a variable: { ...object, [...keyPath]: value } Currently, it is trivial to have one-level access be abstracted out into a variable, but multi-level access necessarily requires either manual syntax construction or a reduce. With the above, keys and keyPaths would be equally treatable as data. |
One additional thought, if we are using spread syntax instead of with objects, we could potentially do the same with arrays. It is already the case that push becomes simpler than with the [...array, 5] /* vs. */ array with push(5) [5, ...array] /* vs. */ array with enqueue(5) But we could do something similar for insertion as well: [...array, [1] = 7] /* vs. */ array with [1] = 7 |
That's an interesting approach, and it could be a proposal on its own indeed... Let's try to collect feedback on this one |
@rickbutton Not quite true, actually. Perl allows |
Perhaps not an essential reason, but "with" already has a bad wrap in JavaScript. Googling "JavaScript with" is going to bring up a lot of confusing "Don't ever use this feature!", and "with puts v8 into a slow path!" type articles. Separately I find it hard to parse the parenthesis-less version in a function call (vs. the commas delimiting arguments). |
With the new changes in the syntax (i.e. const x = #{a: 1, b: 2}
// Const update
#{...x, a: 5} === #{a: 5, b: 2}
// Normal spread
{...x, a: 5} !== #{a: 5, b: 2}
deepEqual({...x, a: 5}, {a: 5, b: 2}) === true |
Spread and |
I'm not sure I'm entirely sold on the proposal of this |
Must I don't believe this proposal needs the I would prefer to see this proposal advance on its own, and the |
Related: #19 |
Me too. It is really diluting the usefulness of the documentation to show how the const/immutable values feature works. I would prefer it to be completely removed from this proposal, but if not, could we at least separate it out of the initial examples and try to focus on the primary feature (const/immutable values) there? @rricard |
btw, I don't really like how this feature invents totally new syntax and semantics. Would it be possible to build on the existing syntax and semantics of object literals?
|
It seems the key benefit of I wonder if it's possible to start with a much common way to achieve the similar functionality: import {update} from 'std:const-utils'
let foo = #[{a: 1}, {a: 2}]
foo = update(foo, v => {
// v is a special proxy-like object to receive the operations
v[0].a = 2
v[1].a++
v.push(#{a: 4})
})
assert(foo === #[{a: 2}, {a: 3}, {a: 4}]) Of coz |
An alternative could be to use a destructuring-like syntax: // objects
const a = obj pick {b: 5}
// arrays
const b = obj pick [0, , 2]
// nested
const c = obj pick {b: pick [0]} |
If we feel the need for a keyword (not that I think it should be part of the proposal), I think const newRecord = myRecord where prop = 'foobar'
// or
const newRecord = myRecord where {prop: 'foobar'} |
Ok, I think we derived a lot from the original issue opened by dan and we addressed things since then. We're not considering other keywords that are not in the reserved keywords list. So things like |
Would it be appropriate for me to open a separate issue for "should I don't believe I've seen any justification for including it in this proposal. |
I'm not exactly sure where I should ask this question. Perhaps Github Discussions would be a better place if that was enabled for this repo, but here it goes: How will |
The spec wouldn't define that; it'd be up to each implementation. |
Right. Just using the |
I have another question: How would one distinguish between reference-equality and value-equality? |
As primitives, they wouldn’t ever have reference equality/identity. |
I was picturing that the syntax would be
obj with .prop = value
, and I see the syntax here asobj with prop = value
. To me, seeingprop
bare like that makes me think of a lexically scoped variable. Are you sure about omitting the.
?The text was updated successfully, but these errors were encountered: