为模块中的每个子类型生成一个方法

Generating a method for each subtype within a module

(为后代转自 Julia slack)

假设我有一些常量,例如

const FooConst = 1
const BarConst = 2

我也有一些结构

struct Foo end
struct Bar end

我现在想为每个结构定义一个方法来查找该常量

f(::Type{Foo}) = FooConst
f(::Type{Bar}) = BarConst

我如何使用元编程实现最后一个块?我实际上是在尝试将 Const 添加到 Struct 名称的末尾,然后在代码中查找

...

...(this) 在模块外工作,但在我的模块中不导出常量。导入我的模块后,f 无法查找常量。 MWE 在这里:

module M
import InteractiveUtils: subtypes
export Foo, Bar, f

abstract type Super end
struct Foo <: Super end
struct Bar <: Super end

const FooConst = 1
const BarConst = 2

for T in subtypes(Super)
    @eval f(::Type{$T}) = $(Symbol(T, "Const"))
end

end # module

然后在我的 REPL 中:

julia> using Main.M
julia> f(Foo)
ERROR: UndefVarError: Main.M.FooConst not defined
Stacktrace:
 [1] f(::Type{Foo}) at ./none:11
 [2] top-level scope at none:0

但是我可以直接访问它:

julia> Main.M.FooConst
1

来自 Julia slack 上的 Mason Protter:

梅森·普罗特 2:26 下午 @Sebastian Rollen 问题是 Symbol(T, "const")。这实际上最终扩展到 Symbol("Main.Foo.FooConst")Symbol("Main.Foo.BarConst") 而不是 Symbol("FooConst")Symbol("BarConst") 分别。您可以使用 Symbol(nameof(T), "Const") 修复该问题,如下所示:

module M
import InteractiveUtils: subtypes
export Foo, Bar, f

abstract type Super end
struct Foo <: Super end
struct Bar <: Super end

const FooConst = 1
const BarConst = 2

for T in subtypes(Super)
    @eval f(::Type{$T}) = $(Symbol(nameof(T), "Const"))
end

end # module

julia> using .M;  f(Foo)
1

julia> f(Bar)
2

确保在 运行 此代码之前重新启动 Julia,否则 Julia 将继续使用旧版本模块中导出的函数。