# Return handles to functions from caller-namespace from a macro

**URL:** https://discourse.julialang.org/t/return-handles-to-functions-from-caller-namespace-from-a-macro/129315
**Category:** General Usage
**Created:** [May 25, 2025, 10:57am UTC](https://discourse.julialang.org/t/return-handles-to-functions-from-caller-namespace-from-a-macro/129315 "2025-05-25T10:57:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wave\_and\_matter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wave_and_matter/32/202530_2.png) [@wave\_and\_matter](https://discourse.julialang.org/u/wave_and_matter)
#### Post date: [May 25, 2025, 10:57am UTC](https://discourse.julialang.org/t/return-handles-to-functions-from-caller-namespace-from-a-macro/129315/1 "2025-05-25T10:57:07Z")

</div>

I have made a minimal working example of a macro that returns a list of handles for the functions it receives as input. Obviously this can be achieved more easily without a macro, but let us assume there is a more complicated task where I need a macro to be able to return working function handles in some data structure. The handles do not have to work inside the macro itself, but they shall point to the respective function in the caller namespace after evaluation.

**Macro demo**

```julia
using MacroTools
macro fun_demo(ex)
    return [eval(x) for x in rmlines(ex).args]
end

```

**Example use**

```julia
f(x)=x
g(x)=x²
@fun_demo begin
    f
    g
end

```

**Example output**

```julia
2-element Vector{Function}:
 f (generic function with 1 method)
 g (generic function with 1 method)

```

**The question**  
The problem with the implementation is, that (a) you are not supposed to use eval inside a macro and (of more practical importance) (b) it will not work anymore if the macro is inside a module because the functions are not known inside the module.  
Thus my question is: How would you achieve the same outcome as above with cleaner code and most importantly in a way that works from within a module?

**Some ideas / things i have tried**  
I have been playing with “esc” without any working results. I could also imagine that instead of returning the vector of function handles directly, you need to return an expression that will generate the vector of function handles once it is returned & evaluated at the call site - but I do not know how to pull it off. A simple “solution” would also be to just return the symbols and then have some external function “instantiate\_function\_handles” that you have to call on the resulting vector… But this also seems unelegant and requires the user of the macro to call a helper function on top of the macro.

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [May 25, 2025, 12:22pm UTC](https://discourse.julialang.org/t/return-handles-to-functions-from-caller-namespace-from-a-macro/129315/2 "2025-05-25T12:22:09Z")

</div>

I’m not entirely sure which context you want to use this in, but something like this:

```julia
macro fun_demo(funs...)
    return Expr(:vect, funs...) |> esc
end

julia> @fun_demo sin cos
2-element Vector{Function}:
 sin (generic function with 14 methods)
 cos (generic function with 14 methods)

```

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [May 25, 2025, 12:30pm UTC](https://discourse.julialang.org/t/return-handles-to-functions-from-caller-namespace-from-a-macro/129315/3 "2025-05-25T12:30:14Z")

</div>

The idea here is to look at what kind of `Expr` creates a vector:

```julia
julia> dump(:([sin, cos]))
Expr
  head: Symbol vect
  args: Array{Any}((2,))
    1: Symbol sin
    2: Symbol cos

```

An `Expr` takes the `head` argument, the rest forms a vector of arguments, which in our case are symbols. So the macro should construct an `Expr(:vect, ...list of symbols...)`. These symbols are in the input `funs...`, or you may create them in some other fashion. So `Expr(:vect, funs...)` does the trick. Then pipe it through `esc` so the macro sanitizer does not add module names and stuff.

Alternatively you can create the `Expr` iteratively:

```julia
macro fun_demo(funs...)
    ex = :([])
    for f in funs
        push!(ex.args, f)
    end
    return ex |> esc    
end

```

---

<div class="post-metadata">

### Author: ![wave\_and\_matter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wave_and_matter/32/202530_2.png) [@wave\_and\_matter](https://discourse.julialang.org/u/wave_and_matter)
#### Post date: [May 25, 2025, 3:18pm UTC](https://discourse.julialang.org/t/return-handles-to-functions-from-caller-namespace-from-a-macro/129315/4 "2025-05-25T15:18:56Z")

</div>

Thanks, basically this is doing the job.  
I added a version a bit closer to my original example and with a module to show that it works:

```julia
module FunDemo3
    using MacroTools
    macro fun_demo3(ex)
        return Expr(:vect, rmlines(ex).args...) |> esc
    end
    export @fun_demo3
end

using .FunDemo3
f(x)=x
g(x)=x²
@fun_demo3 begin
    f
    g
end

```
