Revise.jl 的自动删除 method 行为的调查

首先创建并激活一个示例 module:

julia> using Revise

(v1.0) pkg> generate Example
Generating project Example:
    Example\Project.toml
    Example/src/Example.jl

(v1.0) pkg> activate Example

打开编辑器,更改 Example.jl 为:

module Example

greet() = print("Hello World!")

abstract type AbstractA end
foo(x::AbstractA, y::Integer) = 1

end # module

REPL 中 foo 的 methods 为:

julia> using Example
julia> methods(Example.foo)
# 1 method for generic function "foo":
[1] foo(x::Example.AbstractA, y::Integer) in Example at

在编辑器里,注释掉这一行 foo:

module Example

greet() = print("Hello World!")

abstract type AbstractA end
# foo(x::AbstractA, y::Integer) = 1

end # module

REPL 中 foo 的 methods 为:

julia> methods(Example.foo)
# 0 methods for generic function "foo":

可以正常删除。

继续修改为:

module Example

greet() = print("Hello World!")

abstract type AbstractA end
struct B <: AbstractA end
foo(x::B, y::Integer) = 1

end # module

REPL 中 foo 的 methods 为:

julia> methods(Example.foo)
# 1 method for generic function "foo":
[1] foo(x::Example.B, y::Integer) in Example at

@Roger 这个是正常预期的 behavior

1 个赞

Revise.jl 文档里有写:

However, to find the right method(s) to delete, Revise needs to be able to parse source code to extract the signature of the to-be-deleted method(s). Unfortunately, a few valid constructs are quite difficult to parse properly. For example, methods generated with code:

for T in (Int, Float64, String)   # edit this line to `for T in (Int, Float64)`
    @eval mytypeof(x::$T) = $T
end

will not disappear from the method lists until you restart.

即如果无法正确解析源码中的 method signature,那么 method 自动删除就会失败,比如这个动态生成代码的例子。

2 个赞

嗯,我是用了 eval,OK,看来不是因为不能自动删除,是动态生成的问题。

1 个赞