-
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
Set-builder inspired notation for updates #{ x | property = n }
#44
Comments
Splitting this out into a comment since it opens the scope up significantly, but there are even further variations on this idea, like: let migratedUser = #{ ...user, ...userSettings, admin: false | user => {
user.verification.email = false;
user.verification.phone = false;
user.profile.visibility = 'private';
}}; ... where let user = #{ name: 'Jane' };
let userSettings = #{ profile: { visibility: 'public', darkMode: false } };
let migratedUser = #{ ...user, ...userSettings, admin: 'false' | user => {
user.verification.email = false;
user.verification.phone = false;
user.profile.visibility = 'private';
if (user.profile.darkMode) {
console.log('User is probably a programmer');
}
}};
assert(migratedUser === #{
name: 'Jane',
profile: {
visibility: 'private',
darkMode: false
},
admin: false,
verification: {
email: false,
phone: false,
}
}); |
I've been reluctant to suggest this personally because of one main question: how would you spec out things like Edit: I mean specifically with a |
My thoughts were just But your point about it looking a little un-JS makes me wonder if then perhaps a more Immer-like syntax would be better: (While the first syntax (that doesn't accept functions) can just be desugared into a series of spreads (though preferably there'd be some sort of black-boxy optimisation going on), the second syntax implies that some sort of intermediate mutable object (with otherwise identical rules to the record) is made, passed to the function, then returned and compared for differences against the record.) |
@ceigey would you be OK with the following, as you're suggesting two orthogonal changes?
|
Hi @mheiber, happy to oblige; I'll remove the Immer related bits and make this as simple as possible. |
Thanks for your help! |
I think this will be best to discuss in a follow-on proposal. Let's add something in the README explicitly pointing to this possibility. |
I've added a section to the proposal that references this as a possibility for a follow-up proposal. Thank you for this! |
Some discussion has been brought up about whether
with
is appropriate. So here's a (sub)proposal inspired by Mint and Set builder notation.Note this issue has been edited to keep things relevant.
Summary
The following syntax is proposed as a variation on
x with .prop = n
:#{ x | prop = n, sub.prop = m }
The pipe could alternatively be replaced with "with", which in this case would be further contextualised by the
#{ }
brackets.Inspiration
Mint uses, or rather, will use a set-builder inspired notation for record updates. This set builder notation is quite well known throughout the world of computer science, and while not all JavaScript developers have compsci backgrounds, there is a lot of inertia there.
This is a proposed example of a record update in Mint:
(as taken from the Mint documentation, 18th Aug 2019)
Proposed use in JavaScript
Here, I would like to consider the above adapted for this JavaScript proposal, as follows:
Thus:
#{ RECORD | ASSIGNMENT_1, ..., ASSIGNMENT_N }
.In the
ASSIGNMENTS
section of the notation, direct properties ofRECORD
are now treated as variables which can be updated with any assignment operator (e.g.=
,++
,+=
, etc). In addition, you can update nested properties too.This requires the reuse of one reserved symbol,
|
, normally the bitwise-or orperator, and introduces a new context for the interpreter/compiler.The use of
|
here appears unambiguous, as I'm not sure how a bitwise-or could even be used in this context; to my knowledge, you'd always have aSyntaxError
attempting to write something like this.However, the use of
with
is also unambiguous here too (then again,with
is fairly unambiguous in the original proposal, but at least in this case you don't need to dowith .name = 'Bob'
where a leading.
is introduced). This should perhaps be separated out into a separate issue, perhaps at a later stage.Comparisons
Compared to the
with
syntax already proposed:Compared to possible use of
+
and-
for updates (based on an idea shared by @ljharb in #41, see here):Note that here, any additional properties of
address
are not overwritten, onlylocation
is updated.Pros and cons
Pros
Cons
javascript record with
is perhaps easier to search for thanjavascript record long-sticky-thing
|
is still ambiguous in search engine results, just likewith
{ ... | ..... }
too.The text was updated successfully, but these errors were encountered: