我有同一个函数名称的多个函数/调度。我想确保它们全部出口。我是否只需要在导出语句中包含函数的名称,然后让 Julia 完成其余的工作?
例子:
function hello(a::Int64, b::Int64)
#nothing
end
function hello(a::Bool, b::Bool)
#nothing
end
export hello
Run Code Online (Sandbox Code Playgroud)
只要这样做,这两个都会被导出吗export hello
?
这是正确的。实际上,没有任何版本的export
语句允许您选择导出哪种方法。您导出该函数。
下面是一些说明该行为的代码:
julia> module FooBar
export foo
foo(x::Int) = 2
foo(x::Char) = 'A'
end
Main.FooBar
julia> foo
ERROR: UndefVarError: foo not defined
julia> @which foo
ERROR: "foo" is not defined in module Main
Stacktrace:
[1] error(::String) at .\error.jl:33
[2] which(::Module, ::Symbol) at .\reflection.jl:1160
[3] top-level scope at REPL[15]:1
julia> using .FooBar
julia> @which foo
Main.FooBar
julia> methods(foo)
# 2 methods for generic function "foo":
[1] foo(x::Char) in Main.FooBar at REPL[13]:4
[2] foo(x::Int64) in Main.FooBar at REPL[13]:3
Run Code Online (Sandbox Code Playgroud)