Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will exporting a function name export all the different function versions in Julia?

Tags:

julia

I have multiple functions/dispatches for the same function name. I want to make sure they are all exported. Do I just need to include the name of the function in the export statement and let then Julia do the rest?

Example:

function hello(a::Int64, b::Int64)
   #nothing
end

function hello(a::Bool, b::Bool)
   #nothing
end

export hello

Will both of these be exported by just doing export hello?

like image 756
logankilpatrick Avatar asked Apr 25 '20 16:04

logankilpatrick


2 Answers

Yes, you export the function name, and that function has two method in this case, and both of them will be available.

And to add, there is no way to export a subset of the methods.

like image 50
fredrikekre Avatar answered Nov 10 '22 04:11

fredrikekre


That's right. Actually, there is no version of the export statement that would allow you to pick which method to export. You export the function.

Here is some code which illustrates the behavior:

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
like image 5
essenciary Avatar answered Nov 10 '22 04:11

essenciary