【发布时间】:2015-07-25 12:50:46
【问题描述】:
我将用一个最小的例子来解释我的问题。假设我有三个文件:
A.jl
module A
export Atype, f
type Atype
end
f = function(x::Atype)
println("f called with A")
end
end #module
B.jl
module B
export Btype, f
type Btype
end
f = function(x::Btype)
println("f called with B")
end
end #module
Main.jl
using A
using B
main = function()
x = Atype()
f(x)
end
main()
这里我有两个版本的f 函数。如果我正确理解了多次分派的概念,则应该在运行时扣除应该使用哪个版本。因此,我预计运行 Main.jl 会打印 f called with A。不幸的是,我得到了
$ julia Main.jl
ERROR: type: anonymous: in typeassert, expected Btype, got Atype
in include at /usr/bin/../lib64/julia/sys.so
in process_options at /usr/bin/../lib64/julia/sys.so
in _start at /usr/bin/../lib64/julia/sys.so
while loading /home/grzes/julia_sucks/Main.jl, in expression starting on line 9
如果我注释掉using B,它可以正常工作。显然,来自 B.jl 的 f 覆盖了来自 A.jl 的 f。
所以,问题是:问题出在哪里?在我的方法中还是在我使用的 Julia 版本中(0.3.7)?我该如何规避呢?
请注意,将using A 替换为import A 并使用完全限定名称(例如A.f)并不是一个好的解决方案。它与多分派的症结相矛盾——在编译时我不知道应该使用A.f 还是B.f。
【问题讨论】: