Is @which lying to me?

#1

MWE:

julia> struct MyNumber
       value::Float64
       end

julia> Base.:^(x::MyNumber,n::Int) = MyNumber(x.value^n)

julia> @which x^-1
^(x::MyNumber, n::Int64) in Main at REPL[2]:1

julia> x = MyNumber(rand())
MyNumber(0.9658905866711973)

julia> x^-1
ERROR: MethodError: no method matching inv(::MyNumber)
Closest candidates are:
  inv(::Complex{Float64}) at complex.jl:431
  inv(::BigFloat) at mpfr.jl:471
  inv(::Integer) at int.jl:56

It should not be calling inv(::MyNumber), right?

0 Likes

#2

Yes. https://github.com/JuliaLang/julia/issues/21014

3 Likes

#3

Thanks :raised_hands:

The solution is not clear to me though. Should I overload inv and define ^ in terms of inv? :thinking:

0 Likes

#4

Add
Base.inv(x::MyNumber) = MyNumber(inv(x.value))

1 Like