Lets define call syntax for Int
(i::Int)(n::Number) = i*n
3(4) # 12
now I can list the methods by using Int.name.mt
which will output
# 1 method for generic function "(::Int64)":
[1] (i::Int64)(n::Number) in Main at none:1
The goal is to delete this again, however I fail trying so. Best guess is just calling empty!()
on it, but that is not defined… Next guess is using Base.delete_method
, however that does not work either…
Base.delete_method(collect(Base.MethodList(Int.name.mt))[1])
3(4) # 12
Does anyone know how I can empty the methodtable of a Type, i.e. getting Int.name.mt
empty again?
another try… also failing
for m ∈ methods(1)
Base.delete_method(m)
end
3(4) # 12
delete_method
works fine:
julia> sin("asd")
ERROR: MethodError: no method matching sin(::String)
Closest candidates are:
sin(::BigFloat) at mpfr.jl:683
sin(::Missing) at math.jl:1056
sin(::Complex{Float16}) at math.jl:1005
...
Stacktrace:
[1] top-level scope at none:0
julia> Base.sin(::String) = "ha"
julia> sin("asd")
"ha"
julia> Base.delete_method(which(sin, (String,)))
julia> sin("asd")
ERROR: MethodError: no method matching sin(::String)
Closest candidates are:
sin(::BigFloat) at mpfr.jl:683
sin(::Missing) at math.jl:1056
sin(::Complex{Float16}) at math.jl:1005
...
Stacktrace:
[1] top-level scope at none:0
What you’re seeing is normal behaviour – a number (e.g. 3
) juxtaposed with an expression (e.g. (4)
) is defined as multiplication. Your overload didn’t actually take effect at all:
julia> (i::Int)(n::Number) = i+n
julia> 3(4)
12 # should be 7 if the method above was called
4 Likes
what a coincidence :'D
thanks for pointing that out!
an adapted example indeed works. Thanks a lot!
julia> "hi"("ho")
ERROR: MethodError: objects of type String are not callable
Stacktrace:
[1] top-level scope at none:0
julia> (i::String)(n::String) = i * n
julia> "hi"("ho")
"hiho"
julia> for m ∈ Base.MethodList(String.name.mt)
Base.delete_method(m)
end
julia> "hi"("ho")
ERROR: MethodError: objects of type String are not callable
Stacktrace:
[1] top-level scope at none:0