Is @which lying to me?

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?

Yes. `@which` and `@code_warntype` don't show `^Val{p}` lowering · Issue #21014 · JuliaLang/julia · GitHub

3 Likes

Thanks :raised_hands:

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

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

1 Like