diff --git a/src/Statistics.jl b/src/Statistics.jl index 911f724d..64ba7560 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -107,9 +107,13 @@ if !isdefined(Base, :mean) function mean(f::Number, itr::Number) f_value = try f(itr) - catch MethodError - rethrow(ArgumentError("""mean(f, itr) requires a function and an iterable. - Perhaps you meant middle(x, y)?""",)) + catch err + if err isa MethodError && err.f === f && err.args == (itr,) + rethrow(ArgumentError("""mean(f, itr) requires a function and an iterable. + Perhaps you meant mean((x, y))?""")) + else + rethrow(err) + end end Base.reduce_first(+, f_value)/1 end diff --git a/test/runtests.jl b/test/runtests.jl index 6599b46d..6903f90b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -173,12 +173,18 @@ end @test isnan(@inferred mean(Iterators.filter(x -> true, Float64[]))) # using a number as a "function" - @test_throws "ArgumentError: mean(f, itr) requires a function and an iterable.\nPerhaps you meant middle(x, y)" mean(1, 2) + @test_throws "ArgumentError: mean(f, itr) requires a function and an iterable.\nPerhaps you meant mean((x, y))" mean(1, 2) struct T <: Number x::Int end - (t::T)(y) = t.x * y - @test @inferred mean(T(2), 3) === 6.0 + (t::T)(y) = t.x == 0 ? t(y, y + 1, y + 2) : t.x * y + @test mean(T(2), 3) === 6.0 + @test_throws MethodError mean(T(0), 3) + struct U <: Number + x::Int + end + (t::U)(y) = t.x == 0 ? throw(MethodError(T)) : t.x * y + @test @inferred mean(U(2), 3) === 6.0 end @testset "mean/median for ranges" begin