andyferris / Dictionaries.jl

An alternative interface for dictionaries in Julia, for improved productivity and performance
Other
278 stars 28 forks source link

Issue with constructing Dictionary from dictionary with subtypes of abstract type #89

Closed curtd closed 2 years ago

curtd commented 2 years ago

There's an issue with the convenience function dictionary when the indices are distinct subtypes of an abstract type, i.e.,

abstract type A end
struct B <: A end
struct C <: A end
b = B()
c = C()

d = dictionary([b => 1, c => 2])
# 2-element Dictionary{A, Int64}
# B() │ 94489280530
# C() │ 2

d = dictionary([b => 1, c => 2])
# 2-element Dictionary{A, Int64}
# B() │ 94489280530
# C() │ 2

d = dictionary([b => 1, c => 2])
# 2-element Dictionary{A, Int64}
# B() │ 0
# C() │ 2

other methods of constructing the Dictionary seem fine, i.e.,

d = Dictionary{A,Int}([b,c],[1,2])
# 2-element Dictionary{A, Int64}
# B() │ 1
# C() │ 2

d = Dictionary{A,Int}()
# 0-element Dictionary{A, Int64}

insert!(d, b, 1)
# 1-element Dictionary{A, Int64}
# B() │ 1

insert!(d, c, 2)
# 2-element Dictionary{A, Int64}
# B() │ 1
# C() │ 2
andyferris commented 2 years ago

Thanks. I find the promotion logic tricky to get right. I’ll take a look

andyferris commented 2 years ago

This one was actually fixed on master but unreleased; I've just made a new release