-
-
Notifications
You must be signed in to change notification settings - Fork 5.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
add merge() for tuples #57052
add merge() for tuples #57052
Conversation
julia> merge((a=1, b=2, c=3), (c=4, b=5))
(a = 1, b = 5, c = 4) I appreciate the idea and the proposal but I do not think this method is consistent with the generic function's semantics. If there's a compelling usecase, perhaps we could find an alternative way to meet it. |
Indeed, the keys/indices story is a bit of a mixup in Julia – but that's a general issue, not really specific to this PR. |
@@ -349,6 +349,9 @@ function _front(v, t...) | |||
(v, _front(t...)...) | |||
end | |||
|
|||
|
|||
merge(a::Tuple, b::Tuple) = (b..., last(a, length(a) - length(b))...) |
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.
julia> merge((), (7,))
ERROR: ArgumentError: Take length must be non-negative
Fix:
merge(a::Tuple, b::Tuple) = (b..., last(a, length(a) - length(b))...) | |
merge(a::Tuple, b::Tuple) = (b..., last(a, max(0, length(a) - length(b)))...) |
I feel a deep discomfort with this method, but I must mention @LilithHafner that the NamedTuple case also violates the semantics you mention, since julia> nt = (a=1, b=2, c=3);
julia> [x for x in nt]
3-element Vector{Int64}:
1
2
3
julia> Dict(nt)
ERROR: ArgumentError: AbstractDict(kv): kv needs to be an iterator of 2-tuples or pairs
Stacktrace:
[1] _throw_dict_kv_error()
[...] If the claim is that NamedTuples are key-value pairs because you can use All of that said, I'm rather happy with |
Maybe it'd help @aplavin if you could explain why you want this? Is it just because you feel it makes sense to exist, or is there a use-case for it? Perhaps a case-study of how this would be used would help with some of the apprehension people feel about it. I'm struggling to picture what good this method could do, whereas I can imagine a lot of situations where I'd rather this threw a method error because I accidentally passed a I also just think the meaning of this is likely to confuse users, even if it's actually a very simple and straightforward idea "from first principles". |
I do not want tuples merging by positional overwrite.
That seems less than ok. |
Tbh, don't see how this behavior is significantly different from julia> nt1 = (a=1, b=2, c=3)
julia> nt2 = (c=4, a=5)
julia> t1 = values(nt1)
julia> t2 = values(nt2)
julia> nt1[keys(nt2)]
(c = 3, a = 1)
julia> t1[keys(t2)]
(1, 2)
|
Triage agrees that this makes sense. However, all new features must have strong motivation (i.e. usecase where this is helpful) and we don't see that here. |
Interesting – didn't expect this to be universally required. I mean, it makes sense for adding new types/functions of course, but adding new methods for Base functions + Base types is different: this can only be done elsewhere with piracy. My usecase is quite straightforward, I have a default set of options |
I guess the problem there is "reasonable" --- maybe sometimes a method is obvious, but here we did not see why a tuple would be used as a dictionary-like structure. Why would options be indexed positionally? Could you give some more details about the use case? In any case, it is clear that we need to have a strong default preference for not adding things, or the bloat and maintenance load will just keep growing. This is needed to counter the natural ratchet effect of adding things usually not being breaking, but deleting things being breaking. |
merge()
makes sense for any indexed collection, while it's currently only defined for dicts and namedtuples.Here, I define it for regular tuples, with the same semantics:
Potentially, the
merge
docstring should also be generalized to say that it merges all collections in the same way –merge(a, b)[i] == b[i]
fori in keys(b)
, anda[i]
for otheri
.Will add tests and update docs if this addition is ok. Maybe also define for vectors?