# How to export functions from an array in a module?

**URL:** <https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 13, 2019, 9:30am UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234 "2019-06-13T09:30:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [June 13, 2019, 9:30am UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234/1 "2019-06-13T09:30:58Z")

</div>

I am trying to do something like this

```julia
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
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
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
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?

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 13, 2019, 10:52am UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234/2 "2019-06-13T10:52:11Z")

</div>

Have you considered this snippet:

> [@Exportall?](https://discourse.julialang.org/t/exportall/4970/14):
>
> The following simple snippet seems to work: module Foo foo(x) = x+1 bar(x) = x-1 # exportall: for n in names(current\_module(), true) if Base.isidentifier(n) && n ∉ (Symbol(current\_module()), :eval) @eval export $n end end end

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 13, 2019, 11:16am UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234/3 "2019-06-13T11:16:16Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 13, 2019, 11:32am UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234/4 "2019-06-13T11:32:07Z")

</div>

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)

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [June 13, 2019, 11:39am UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234/5 "2019-06-13T11:39:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [June 13, 2019, 12:23pm UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234/6 "2019-06-13T12:23:15Z")

</div>

> [@jishnub](#):
>
> Didn’t know about `nameof` .

I learned about it via `methodswith`:

```julia
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

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 13, 2019, 2:29pm UTC](https://discourse.julialang.org/t/how-to-export-functions-from-an-array-in-a-module/25234/7 "2019-06-13T14:29:11Z")

</div>

> [@ffevotte](#):
>
> I learned about it via `methodswith` :

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).
