删除 Julia 中的方法

Geo*_*ery 6 julia

我有一个有 2 种方法的函数

function foo(a::Integer) 42 end
function foo(a::String) 24 end

foo(2)
42

foo("a")
24
Run Code Online (Sandbox Code Playgroud)

如何仅删除这两种方法之一?

Geo*_*ery 6

有一种类型Method。该类型的实例指的是特定函数的特定方法。

particular_method = @which foo(2)
foo(a::Integer) in Main at /home/js/Documents/Julia/Matching/Query_Creation.jl:75

typeof(particular_method)
Method
Run Code Online (Sandbox Code Playgroud)

这是使用此类对象删除方法的方法:

Base.delete_method(particular_method)

foo(2)
ERROR: MethodError: no method matching foo(::Int64)
Run Code Online (Sandbox Code Playgroud)