In Julia 1.x, it seems like isequal(convert(T, x), x)
is usually true for Base and standard library types. However, there is one notable exception: Char
to Int
and Int
to Char
. It is a little odd that this works:
julia> struct A
x::Int
end
julia> A('a')
A(97)
Dict
and Set
have an explicit workaround for cases where isequal(convert(T, x), x)
is not true:
function setindex!(h::Dict{K,V}, v0, key0) where V where K
if key0 isa K
key = key0
else
key = convert(K, key0)::K
if !(isequal(key, key0)::Bool)
throw(ArgumentError("$(limitrepr(key0)) is not a valid key for type $K"))
end
end
setindex!(h, v0, key)
end
As you can see, the setindex!
method tests to see whether the converted key isequal
to the input key. You can see this check in action here:
julia> push!(Set('a'), 99)
ERROR: ArgumentError: 99 is not a valid key for type Char
Stacktrace:
[1] setindex!(h::Dict{Char, Nothing}, v0::Nothing, key0::Int64)
@ Base ./dict.jl:376
[2] push!(s::Set{Char}, x::Int64)
@ Base ./set.jl:67
[3] top-level scope
@ REPL[21]:1
Would it make sense in Julia 2.0 to add a note to the convert
docstring that says that isequal(convert(T, x), x)
should always be true? Are there any other examples of Base or standard library types for which isequal(convert(T, x), x)
is not true?