Re-evaluate function in another module

Consider the following:

module A
    export foo
    whoami() = "A"
    foo() = whoami()
end

module B
    using Main.A
    whoami() = "B"
end

B.foo() # "A"

I understand why that’s the case but is there a way to call A.foo “in the context of B” i.e. effectively calling B.whomai() and returning "B"? (short of re-defining foo manually in B). I tried using @__MODULE__ in the definition of foo but either used it incorrectly or it’s not the right way to go.

The following does part of what I want but I’d like not to modify A outside of the scope of B.

module B
    using Main.A;
    import Main.A.whoami
    whoami() = "B"
end

B.foo() # "B"  ok 
A.foo() # "B"  nok

Thanks

Not elegant, but:

module A
   export foo
   whoami() = @__MODULE__
   foo = :(eval(:($whoami)))
end 

module B
   # Note that B knows nothing about A
   whoami() = @__MODULE__
end

A.eval(A.foo)()  # Main.A
B.eval(A.foo)()  # Main.B
2 Likes