Dictionaries.jl icon indicating copy to clipboard operation
Dictionaries.jl copied to clipboard

`UnorderedDictionary` with more keys than values doesn't throw

Open jariji opened this issue 1 year ago • 3 comments

I expected this to throw an exception.

julia> Dictionary(['a', 'b'], [1])
ERROR: AssertionError: length(values) == length(_values(inds))

julia> UnorderedDictionary(['a', 'b'], [1])
1-element UnorderedDictionary{Char, Int64}
 'a' │ 1

jariji avatar Jan 15 '25 05:01 jariji

Okay, this one is because internally we use zip and iterate that.

Originally zip would error out when the collections had different lengths, but now it is more forgiving (I would have preferred introducing a new funciton, but there you go). Perhaps being this forgiving here would only encourage mistakes.

We should probably fix this by comparing the lengths of the inputs (but only if the iterators possess the HasLength trait...)

andyferris avatar Jan 16 '25 23:01 andyferris

We should probably fix this by comparing the lengths of the inputs (but only if the iterators possess the HasLength trait...)

I think if length(collect(x)) != length(collect(y)) then UnorderedDictionary(x,y) should still error even if x or y doesn't HasLength.

jariji avatar Jan 16 '25 23:01 jariji

For sure - we can just do that check a little earlier with HasLength than without.

andyferris avatar Jan 16 '25 23:01 andyferris