# Confusing MethodError: bad message or bad usage?

**URL:** <https://discourse.julialang.org/t/confusing-methoderror-bad-message-or-bad-usage/39528>\
**Category:** General Usage\
**Created:** [May 15, 2020, 2:46pm UTC](https://discourse.julialang.org/t/confusing-methoderror-bad-message-or-bad-usage/39528 "2020-05-15T14:46:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 15, 2020, 2:46pm UTC](https://discourse.julialang.org/t/confusing-methoderror-bad-message-or-bad-usage/39528/1 "2020-05-15T14:46:52Z")

</div>

The MethodError message sometimes doesn’t make sense, and I’m not sure if it’s a problem with MethodError or the way it’s used.

For example the following fails (as documented in Measurements.jl):

```julia
julia> using SpecialFunctions, Measurements

julia> zeta(2.0 ± 0.1)
ERROR: MethodError: no method matching zeta(::Measurement{Float64})
Closest candidates are:
  zeta(::Number) at /home/user/.julia/packages/SpecialFunctions/Bdhxh/src/gamma.jl:527
  zeta(::Number, ::Number) at /home/user/.julia/packages/SpecialFunctions/Bdhxh/src/gamma.jl:547
  zeta(::BigFloat) at /home/user/.julia/packages/SpecialFunctions/Bdhxh/src/gamma.jl:465
  ...

```

But note how the message is confusing, since there is a candidate `zeta(::Number)` and

```julia
julia> Measurement{Float64} <: Number
true

```

The error comes from the following code in [SpecialFunctions.jl](https://github.com/JuliaMath/SpecialFunctions.jl/blob/fc960c66e56d230a83999d8fa76c2c5bba28441c/src/gamma.jl#L526-L531):

```julia
 function $f(z::Number) 
     x = float(z) 
     typeof(x) === typeof(z) && throw(MethodError($f, (z,))) 
     # There is nothing to fallback to, as this didn't change the argument types 
     $f(x) 
 end

```

My question: Is this an issue with Julia (`MethodError` printing a message that assumes too much), or is it a semantically incorrect use of `MethodError`?

### Similar cases in the standard library

Sometimes `ArgumentError` is used for example in `base/abstractarray.jl`:

```julia
require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1"))

```

and

```julia
checkindex(::Type{Bool}, inds::AbstractUnitRange, i) =
    throw(ArgumentError("unable to check bounds for indices of type $(typeof(i))"))

```

These are helpful messages.

But in other places, `MethodError` is used and leads to the same problem. For example in `base/irrationals.jl`:

```julia
<(::Irrational{s}, ::Irrational{s}) where {s} = false
function <(x::AbstractIrrational, y::AbstractIrrational)
    Float64(x) != Float64(y) || throw(MethodError(<, (x, y)))
    return Float64(x) < Float64(y)
end

```

And in `base/essentials.jl`:

```julia
function tuple_type_tail(T::Type)
  ...
        T.name === Tuple.name || throw(MethodError(tuple_type_tail, (T,)))
  ...
end

```

This last one gives the following message, similarly confusing:

```julia
julia> Base.tuple_type_tail(Int32)
ERROR: MethodError: no method matching tuple_type_tail(::Type{Int32})
Closest candidates are:
  tuple_type_tail(::Type) at essentials.jl:211
Stacktrace:
 [1] tuple_type_tail(::Type) at ./essentials.jl:217
 [2] top-level scope at REPL[56]:1

```

So, bad message or bad use of MethodError?
