Skip to content
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

Base.in and Base.first are limited to arguments of the same eltype of the UniqueVector #24

Open
giopaglia opened this issue Jun 16, 2023 · 0 comments

Comments

@giopaglia
Copy link

giopaglia commented Jun 16, 2023

Hi,

Maybe I am missing something, but I don't understand why Base.in and Base.findfirst are currently implemented so that they expect an item of the same eltype of the UniqueVector, and they perform a conversion when this does not hold:

in(item::T, uv::UniqueVector{T}) where {T} = haskey(uv.lookup, item)
in(item, uv::UniqueVector{T}) where {T} = in(convert(T, item), uv)

findfirst(p::EqualTo{<:T}, uv::UniqueVector{T}) where {T} = get(uv.lookup, p.x, nothing)
findfirst(p::EqualTo, uv::UniqueVector{T}) where {T} = findfirst(isequal(convert(T, p.x)), uv)

I'd like Base.in and Base.findfirst to be a little more flexible. Here are the errors I would like not to show up:

julia> findfirst(isequal(2.5), UniqueVector(1:10))
ERROR: InexactError: Int64(2.5)
Stacktrace:
 [1] Int64
   @ ./float.jl:900 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] findfirst(p::Base.Fix2{typeof(isequal), Float64}, uv::UniqueVector{Int64})
   @ UniqueVectors ~/.julia/packages/UniqueVectors/GLRfv/src/UniqueVectors.jl:68
 [4] top-level scope
   @ REPL[12]:1

julia> 2.5 in UniqueVector(1:10)
ERROR: InexactError: Int64(2.5)
Stacktrace:
 [1] Int64
   @ ./float.jl:900 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] in(item::Float64, uv::UniqueVector{Int64})
   @ UniqueVectors ~/.julia/packages/UniqueVectors/GLRfv/src/UniqueVectors.jl:53
 [4] top-level scope
   @ REPL[8]:1

My desire is in line with the behavior of other AbstractVectors:

julia> findfirst(isequal(2.5), 1:10)

julia> 2.5 in 1:10
false

If I add these two methods, it's probably type piracy, and I get a warning, but then I manage to get what I want:

Base.in(item, uv::UniqueVector) = haskey(uv.lookup, item)
Base.findfirst(p::UniqueVectors.EqualTo, uv::UniqueVector) = get(uv.lookup, p.x, nothing)

Wouldn't it be correct to have them in the package?

Thank you,
GP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant