Skip to content

Commit 2702ef2

Browse files
committed
Allow non-Function callable as combine argument of merge[!]
1 parent 589a6d8 commit 2702ef2

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

base/abstractdict.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Dict{Int64,Int64} with 3 entries:
213213
1 => 0
214214
```
215215
"""
216-
function merge!(combine::Function, d::AbstractDict, others::AbstractDict...)
216+
function merge!(combine, d::AbstractDict, others::AbstractDict...)
217217
for other in others
218218
for (k,v) in other
219219
d[k] = haskey(d, k) ? combine(d[k], v) : v
@@ -313,7 +313,7 @@ Dict{String,Float64} with 3 entries:
313313
"foo" => 0.0
314314
```
315315
"""
316-
merge(combine::Function, d::AbstractDict, others::AbstractDict...) =
316+
merge(combine, d::AbstractDict, others::AbstractDict...) =
317317
merge!(combine, _typeddict(d, others...), others...)
318318

319319
promoteK(K) = K

test/dict.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,9 @@ let
917917
end
918918
end
919919

920+
struct NonFunctionCallable end
921+
(::NonFunctionCallable)(args...) = +(args...)
922+
920923
@testset "Dict merge" begin
921924
d1 = Dict("A" => 1, "B" => 2)
922925
d2 = Dict("B" => 3.0, "C" => 4.0)
@@ -925,6 +928,7 @@ end
925928
@test @inferred merge(+, d1, d2) == Dict("A" => 1, "B" => 5, "C" => 4)
926929
@test @inferred merge(*, d1, d2) == Dict("A" => 1, "B" => 6, "C" => 4)
927930
@test @inferred merge(-, d1, d2) == Dict("A" => 1, "B" => -1, "C" => 4)
931+
@test @inferred merge(NonFunctionCallable(), d1, d2) == Dict("A" => 1, "B" => 5, "C" => 4)
928932
end
929933

930934
@testset "Dict merge!" begin
@@ -939,6 +943,8 @@ end
939943
@test d1 == Dict("A" => 1, "B" => 18, "C" => 32)
940944
@inferred merge!(-, d1, d2)
941945
@test d1 == Dict("A" => 1, "B" => 15, "C" => 28)
946+
@inferred merge!(NonFunctionCallable(), d1, d2)
947+
@test d1 == Dict("A" => 1, "B" => 21, "C" => 36)
942948
end
943949

944950
@testset "Dict reduce merge" begin

0 commit comments

Comments
 (0)