-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Open
Labels
complexComplex numbersComplex numberserror handlingHandling of exceptions by Julia or the userHandling of exceptions by Julia or the usererror messagesBetter, more actionable error messagesBetter, more actionable error messagesrationalsThe Rational type and values thereofThe Rational type and values thereof
Description
Minimal reproducer:
julia> struct A <: Integer end
julia> struct B <: Integer end
julia> Union{A,B}(1//2)
ERROR: MethodError: no method matching nameof(::Type{Union{A, B}})
The function `nameof` exists, but no method is defined for this combination of argument types.
Closest candidates are:
nameof(::Core.IntrinsicFunction)
@ Base reflection.jl:2369
nameof(::Module)
@ Base essentials.jl:401
nameof(::UnionAll)
@ Base reflection.jl:343
...
Stacktrace:
[1] Union{A, B}(x::Rational{Int64})
@ Base ./rational.jl:151
[2] top-level scope
@ REPL[3]:1
It's not valid to call nameof
on an arbitrary subtype of Integer
, because it may be an Union
:
Lines 149 to 152 in 945517b
Bool(x::Rational) = x==0 ? false : x==1 ? true : | |
throw(InexactError(:Bool, Bool, x)) # to resolve ambiguity | |
(::Type{T})(x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num)::T : | |
throw(InexactError(nameof(T), T, x))) |
It seems like this could be fixed by simply replacing the nameof
call with :convert
.
Metadata
Metadata
Assignees
Labels
complexComplex numbersComplex numberserror handlingHandling of exceptions by Julia or the userHandling of exceptions by Julia or the usererror messagesBetter, more actionable error messagesBetter, more actionable error messagesrationalsThe Rational type and values thereofThe Rational type and values thereof