julia icon indicating copy to clipboard operation
julia copied to clipboard

Cases in with convert has dispatch ambiguity

Open bkamins opened this issue 2 years ago • 3 comments

We have the following behaviors:

julia> convert(Union{}, 1)
ERROR: MethodError: convert(::Type{Union{}}, ::Int64) is ambiguous. Candidates:

julia> convert(Missing, 1) # the same - falls back to the previous one internally
ERROR: MethodError: convert(::Type{Union{}}, ::Int64) is ambiguous. Candidates:

@vtjnash - I am pinging you as you have last changed the relevant convert methods.

bkamins avatar Aug 03 '22 07:08 bkamins

Ah, this case might need to be added explicitly to nonmissingtype_checked?

vtjnash avatar Aug 03 '22 18:08 vtjnash

This would solve convert(Missing, 1), but convert(Union{}, 1) problem would still be present (probably no one in practice this will never happen, but just for completeness of definitions in Base I think it would be good to fix it).

Also the same problem as with nonmissingtype_checked you mention is with nonnothingtype_checked:

julia> Base.nonnothingtype_checked(Nothing)
Union{}

bkamins avatar Aug 03 '22 20:08 bkamins

no, that is intentionally ambiguous, since it is often unavoidable by package authors

yep, that method should be updated to be the same also

vtjnash avatar Aug 05 '22 22:08 vtjnash

The issue with convert(Missing, 1) is still present on nightly even with nonmissingtype_checked.

I think we should add:

convert(::Type{Missing}, ::Missing) = missing
convert(::Type{Missing}, ::T) where {T} = throw(ArgumentError("cannot convert value of type $T to `Missing`"))

Of course custom types could override this default conversion rule.

bkamins avatar Sep 24 '22 18:09 bkamins

Yes, this issue is still open, and my comment above still relevant as a possible fix

vtjnash avatar Sep 24 '22 21:09 vtjnash

Ah - you are right. I thought nonmissingtype_checked was changed. Sorry for the confusion.

bkamins avatar Sep 24 '22 21:09 bkamins

Label "types and dispatch" should be applied there

o314 avatar Nov 20 '22 05:11 o314