# Referencing a module on RHS of expression while Metaprogramming

**URL:** <https://discourse.julialang.org/t/referencing-a-module-on-rhs-of-expression-while-metaprogramming/9558>\
**Category:** General Usage\
**Created:** [March 7, 2018, 3:59am UTC](https://discourse.julialang.org/t/referencing-a-module-on-rhs-of-expression-while-metaprogramming/9558 "2018-03-07T03:59:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [March 7, 2018, 3:59am UTC](https://discourse.julialang.org/t/referencing-a-module-on-rhs-of-expression-while-metaprogramming/9558/1 "2018-03-07T03:59:02Z")

</div>

Hi all,

So using meta-programming, I can define a new function `customsum` in a slightly round-about way using interpolation as follows:

```julia
fn = :sum
@eval $(Symbol("custom$(fn)"))(x::Vector{<:Number}) = Base.sum(x)

```

But now, I also want to interpolate onto the RHS of the expression, _and_ include the module name reference (because my actual problem is more complicated than this MWE). I thought the following would work:

```julia
@eval $(Symbol("custom$(fn)"))(x::Vector{<:Number}) = $(Symbol("Base.$(fn)"))(x)

```

but it does not. Calling `customsum` results in `ERROR: UndefVarError: Base.sum not defined`.

I’ve been banging my head against the wall on this for an hour now trying to work out the right syntax, but am admitting defeat and posting here. Any help would be most greatly appreciated.

Cheers,

Colin

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 7, 2018, 4:03am UTC](https://discourse.julialang.org/t/referencing-a-module-on-rhs-of-expression-while-metaprogramming/9558/2 "2018-03-07T04:03:37Z")

</div>

`Base.sum` is not a symbol; it’s an expression (it’s the `.` operator acting on `:Base` and `:sum`). You can show this with `dump()`:

```julia
julia> dump(:(Base.sum))
Expr
  head: Symbol .
  args: Array{Any}((2,))
    1: Symbol Base
    2: QuoteNode
      value: Symbol sum
  typ: Any

```

And you can construct that expression in the same way:

```julia
julia> Expr(:., :Base, QuoteNode(:sum))
:(Base.sum)

julia> eval(Expr(:., :Base, QuoteNode(:sum)))
sum (generic function with 17 methods)

```

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [March 7, 2018, 4:13am UTC](https://discourse.julialang.org/t/referencing-a-module-on-rhs-of-expression-while-metaprogramming/9558/3 "2018-03-07T04:13:43Z")

</div>

Argh! Yes, I see it now. So I could do:

```julia
@eval $(Symbol("custom$(fn)"))(x::Vector{<:Number}) = eval(Expr(:., :Base, QuoteNode(:sum)))(x)

```

or, much more simply:

```julia
@eval $(Symbol("custom$(fn)"))(x::Vector{<:Number}) = Base.$(Symbol("$(fn)"))(x)

```

Thanks so much, that was a huge help.

Cheers,

Colin
