andyferris / Dictionaries.jl

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

`in`/`∈` when item has a type that isn't a subtype of the `Indices` element type #143

Open mtfishman opened 5 months ago

mtfishman commented 5 months ago
julia> using Dictionaries

julia> Indices([1, 2, 3])
3-element Indices{Int64}
 1
 2
 3

julia> 0 ∈ Indices([1, 2, 3])
false

julia> 1 ∈ Indices([1, 2, 3])
true

julia> "x" ∈ Indices([1, 2, 3])
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Int64

Closest candidates are:
  convert(::Type{T}, ::T) where T<:Number
   @ Base number.jl:6
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:84
  convert(::Type{T}, ::Number) where T<:Number
   @ Base number.jl:7
  ...

Stacktrace:
 [1] in(i::String, indices::Indices{Int64})
   @ Dictionaries ~/.julia/packages/Dictionaries/53DRB/src/AbstractIndices.jl:73
 [2] top-level scope
   @ REPL[11]:1

vs.

julia> Set([1, 2, 3])
Set{Int64} with 3 elements:
  2
  3
  1

julia> 0 ∈ Set([1, 2, 3])
false

julia> 1 ∈ Set([1, 2, 3])
true

julia> "x" ∈ Set([1, 2, 3])
false
julia> import Pkg; Pkg.status("Dictionaries")
Status `...`
  [85a47980] Dictionaries v0.4.1
andyferris commented 5 months ago

Yes, I was a originally bit overzealous with convert, which should only really happen on insertion operations. We should fix this.