# Select the same method from different modules

**URL:** <https://discourse.julialang.org/t/select-the-same-method-from-different-modules/6112>\
**Category:** General Usage\
**Created:** [September 27, 2017, 1:18pm UTC](https://discourse.julialang.org/t/select-the-same-method-from-different-modules/6112 "2017-09-27T13:18:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 27, 2017, 1:18pm UTC](https://discourse.julialang.org/t/select-the-same-method-from-different-modules/6112/1 "2017-09-27T13:18:36Z")

</div>

Reposting this from the #helpdesk channel on Slack

I am playing with a way of controlling which module a module should import some function from. Here’s an MWE:

```julia
module Mod1
foo() = "success"
export foo
end

module Mod2
sym = :foo
mod = :Mod1

@eval import $(mod)
@eval $(sym)() = $(Symbol(mod, :., sym))()
@eval export $(sym)
end

```

So Mod2, in theory, takes a function and passes that function to the module signified by `mod`. The problem is, it doesn’t work:

```julia
julia> using Mod2

julia> foo()
ERROR: UndefVarError: Mod1.foo not defined
Stacktrace:
 [1] foo() at ./REPL[2]:6

julia> Mod1.foo
foo (generic function with 1 method)

```

`Mod1.foo` appears to not be defined in the eval-context, though it is defined at the REPL.

The context is that I am looking into ways to change how backends are handled in Plots.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [September 27, 2017, 1:56pm UTC](https://discourse.julialang.org/t/select-the-same-method-from-different-modules/6112/2 "2017-09-27T13:56:32Z")

</div>

This works

```julia
module Mod2
    sym = :foo
    mod = :Mod1

    @eval import $(mod)
    @eval foo = $(Expr(:(.),mod,QuoteNode(sym)))
    @eval export $(sym)
end

```

It generates `foo = Mod1.foo` instead of `foo() = begin Mod1.foo() end`.

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [September 27, 2017, 2:07pm UTC](https://discourse.julialang.org/t/select-the-same-method-from-different-modules/6112/3 "2017-09-27T14:07:45Z")

</div>

If you want to preserve your original functional relationship, you can also do

```julia
@eval $(sym)() = getfield($(mod),sym)()

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [September 27, 2017, 2:20pm UTC](https://discourse.julialang.org/t/select-the-same-method-from-different-modules/6112/4 "2017-09-27T14:20:03Z")

</div>

Amazing 🎉 ❤
