-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add NamedTuple#merge(other : NamedTuple) #4688
Conversation
src/named_tuple.cr
Outdated
def merge(**other : **U) forall U | ||
{% begin %} | ||
{ | ||
{% for k in T %} {% unless U.keys.includes?(k) %} "{{k}}": self[:"{{k}}"],{% end %} {% end %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use {{k.stringify}}
and not "{{k}}"
. I think this is not safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please use {{k.symbolize}}
and not :"{{k}}"
.
I have been long waiting on this. Currently I have my own merge method. Glad to see it built-in stdlib. |
Merges two named tuples into a new one. If both tuples define a value for the same key, the value of the *other* tuple is used.
87e5609
to
9cb6620
Compare
Now using |
I also had a similar case. Having a |
#2634 blows up if both tuples have a value with the same key. |
@Papierkorb that was the main idea, I was trying to compose NamedTuples instead of merging them, so if you duplicate a key, it shouldn't compile. In #2634 you can see a comment explaining why I discarded the |
@emancu I totally see the worth in both methods, and I'd like to see both merged. |
Actually I think there are three reasonable methods of combining two
|
@straight-shoota If I understand correctly there's no PR for |
Of course it is. I'm sorry if this was misunderstanding, it would mean to create a new instance of the same |
@Papierkorb Would be sad to have this PR buried, so 🏓 |
@Papierkorb Could you re-format the source files? It's only failing because of that. |
|
||
# Returns a hash value based on this name tuple's size, keys and values. | ||
# | ||
# See also: `Object#hash`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant line.
@asterite Ah shoot, I'm away from home this week-end. Would it be okay if I do it on Monday? |
@Papierkorb Of course! Take your time :-) |
Like this? Or should I fixup the original commit? |
Hi,
Today in #crystal-lang a user required a possibility to merge two named tuples.
Their use-case was to call methods with named arguments from inside a RESTful API, while being able to easily set default values (or force other arguments to certain values).
Sample code
Using this, one can easily do dynamic invocation on methods, possibly backed by further user-defined macro magic ;)
Behaviour
Takes two named tuples (The other can be kw-args style, or a proper named tuple), and merges keys and values from both into a new named tuple. If both tuples define a value for the same key, the value from the other is kept.
Cya