How to export functions from an array in a module?

I am trying to do something like this

julia> module t
       a(x)=4
       b(x)=x
       for f in [a,b]
       export f
       end
       end
ERROR: syntax: malformed "export" statement
Stacktrace:
 [1] top-level scope at REPL[1]:6245:

In my case I have a lot of functions that I have collected in an array. I would like to loop over it and export all the functions. I’m a bit hazy when it comes to eval and symbols, so any help would be appreciated.

I see that this works

julia> module t
       a(x)=4
       b(x)=x
       for f in [:a,:b]
       @eval export $f
       end
       end
Main.t

julia> using .t

julia> b(3)
3

but this doesn’t

julia> module t
       a(x)=4
       b(x)=x
       for f in Symbol.([a,b])
       @eval export $f
       end
       end
Main.t

julia> using .t

julia> a
ERROR: UndefVarError: a not defined

I suspect that the reason this does not work is that the functions are being referred to by their full path, as it appears here

julia> module t
       a(x)=4
       println(Symbol(a))
       end
Main.t.a
Main.t

So if I could construct the array [:a,:b] from [a,b] I could loop over it. How do I construct this in the module? In any case, is there a different way to export functions from an array within a module?

Have you considered this snippet:

1 Like

You could also consider using nameof to get the name of the function from its type:

julia> module t
       a(x)=4
       b(x)=x
       for f in nameof.([a,b])
           @eval export $f
       end
       end
Main.t


julia> using .t

julia> a
a (generic function with 1 method)

After some try and error
This is considerably less elegant than the previous post, but it goes from [a,b] to [:a,:b] as you planned)

module AModule
a(x)=4
b(x)=x
plusOne(x)=x+1
fn_arr=[a,b,plusOne]
fixedLength=length("Main.AModule")+2
for x in fn_arr
    y=string(x)[fixedLength:end]
    f=Symbol(y)
    #@show f
    #@show typeof(f)            
    #dump(f)        
    #@show fieldnames(typeof(f))        
    @eval export $f
end

for f in names(AModule,all=true) #[a,b]
    #@eval export $f
end
end#end Module

using .AModule
#using Main.AModule

names(Main.AModule)

a(22)

Thanks, this is exactly what I wanted. Didn’t know about nameof.

I learned about it via methodswith:

julia> f() = 1
f (generic function with 1 method)

julia> typeof(f)
typeof(f)

julia> typeof(typeof(f))
DataType

julia> methodswith(DataType)
[1] dump(io::IOContext, x::DataType, n::Int64, indent) in Base at show.jl:1682
[2] fieldname(t::DataType, i::Integer) in Base at reflection.jl:146
[3] fieldnames(t::DataType) in Base at reflection.jl:172
[4] fieldoffset(x::DataType, idx::Integer) in Base at reflection.jl:533
[5] nameof(t::DataType) in Base at reflection.jl:197
[6] parentmodule(t::DataType) in Base at reflection.jl:219
[7] show(io::IO, x::DataType) in Base at show.jl:436
[8] supertype(T::DataType) in Base at operators.jl:44
[9] deserialize(s::Serialization.AbstractSerializer, t::DataType) in Serialization at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Serialization/src/Serialization.jl:1171                                    
[10] serialize(s::Serialization.AbstractSerializer, t::DataType) in Serialization at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Serialization/src/Serialization.jl:548                                      

nameof is not hard to spot in the list

Thank you, thank you so much, I was very annoyed that sometimes I wanted to peruse all methods that took some type of argument and did not believe Julia had a solution like Haskell had (in Haskell case it is a search engine, and so in Julia I searched for a search engine too, not a method).