multiple dispatch - Overloading a function in two files (in Julia) -


i'm gonna explain problem on minimal example. let's have 3 files:

a.jl

module  export atype, f  type atype   end  f = function(x::atype)     println("f called a") end end #module 

b.jl

module b  export btype, f  type btype   end  f = function(x::btype)     println("f called b") end end #module 

main.jl

 using  using b   main = function()     x = atype()     f(x)  end   main() 

here have 2 versions of f function. if understand idea of multiple dispatch correctly, should deducted during runtime version should used. hence, expected running main.jl print f called a. unfortunately, get

$ julia main.jl  error: type: anonymous: in typeassert, expected btype, got atype  in include @ /usr/bin/../lib64/julia/sys.so  in process_options @ /usr/bin/../lib64/julia/sys.so  in _start @ /usr/bin/../lib64/julia/sys.so while loading /home/grzes/julia_sucks/main.jl, in expression starting on line 9 

if comment out using b, works fine. clearly, f b.jl overwrote f a.jl.

so, question is: problem lie? in approach or in version of julia use (0.3.7)? how can circumvent this?

note replacing using a import a , using qualified names (e.g. a.f) not solution. contradicts crux of multiple dispatch -- @ compile time don't know if should use a.f or b.f.

you have make a.f , b.f same function (in example above different functions same name). can overload 1 method in each of modules , multiple dispatch job.

the way achieve either have 1 of modules import function f other (eg import a.f in b) before extending new method, or adding third module c f function both a , b import (you can use dummy signature f(::union()) = nothing never applies create function without adding real method). our extend function other module directly, in

function a.f(x::atype)     println("f called a") end 

this make julia understand 2 f refer same concept, , same object in both modules. adding method modifies generic function object, change visible everywhere f used.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -