-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
adds compiler support for object construction shorthand (full fields initializer) #21559
Conversation
It would be great to also support the shorthand proposed in this forum post: https://forum.nim-lang.org/t/10018#66156
This would be equivalent to:
The idea is to avoid the repetition of the parameter and variable names when they match, which is a pretty common pattern. |
let p = Person( :name, :group ) It is not a valid Nim syntax, which means it needs a new syntax support or parser rewrites. |
|
The original proposal in the thread was to use equals rather than colons (i.e. I find that assigning a variable to a parameter of the same name is a relatively common pattern in my code, and every time I do it the repetition feels somewhat wrong. It would be nice to be able to avoid it if possible. |
No, that looks even worse. :-)
As this PR does it is fine. You can simply leave out the
|
The RFC mentioned proposes automatic mapping of variables to fields with the same name, like in JavaScript. This PR however is a different kind of shorthand that ignores field names and cares only about position (unless explicitly specified). These are two different, probably incompatible, solutions. Why was this chosen instead of the proposed solution in the RFC? That solution is closer to the current object construction syntax, e.g. |
Sorry for writing everywhere at once, but doesn't this change require its own RFC? I have some doubts about this syntax which I expressed in #418 this PR links to, even though this implementation is of a wider scope than the RFC. PS: I deeply respect the hard work @ringabout has been doing for a while, so don't take it personally, the criticism has nothing to do with the quality of what's offered here. |
I think this is a great addition to the language. I know I will use it as soon as its available. I think it will really complement the default object values we have going out as well. |
I guess this should be enabled via another |
@ZoomRmc You're right that it needs an official RFC but please be assured that I've read every concern carefully. |
Edited Sure thing, I will write the RFC. |
This pull request is stale because it has been open for 1 year with no activity. Contribute more commits on the pull request and rebase it on the latest devel, or it will be closed in 30 days. Thank you for your contributions. |
This pull request has been marked as stale and closed due to inactivity after 395 days. |
closes nim-lang/RFCs#418
closes nim-lang/RFCs#517
replaceObjConstr
andfilterObjConstr
into one proc returns enumsWarning
Object constructions with a single unamed field will be interpreted as type conversions, which means
will get a type mismatch error.