# Evaluating a local function within the global namespace

**URL:** <https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 24, 2022, 3:13pm UTC](https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403 "2022-03-24T15:13:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [March 24, 2022, 3:13pm UTC](https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403/1 "2022-03-24T15:13:48Z")

</div>

Hi. I’ve written a tutoral program in which students submit an answer `ans` to an exercise using the format `reply(ans)`. `reply` then passes `ans` deeper into a module within which `ans` is tested for correctness. The thing is, that `ans` might well contain variable names or functions that only exist in the `Main` namespace, and so I want - _within my own module_ - to evaluate `ans` _within the context_ `Main`. I tried something like this:

```julia
module Mod

export reply

iscorrect = (ans -> ans==myvar)

function reply( ans)
# Alternative 1:
iscorrect( Core.eval(Main,:ans))
# Alternative 2:
Core.eval(Main,:(iscorrect( ans)))
end

end # of Mod

using .Mod
myvar = 3
reply( 3)

```

I now have the two alternatives shown in the body of `reply()`: I can either evaluate `:ans` in Main or I can evaluate `:(iscorrect(ans))` in Main. In the first case, `iscorrect()` doesn’t know about the existence of `myvar`, and in the second case, `Main` doesn’t know anything about the existence of `iscorrect()`.

Clearly, in this example case, I could insert a `Main` evaluation of `myvar` within the mapping `iscorrect`; however in the tutorial program, `iscorrect` is just one of hundreds of correctness tests to be applied appropriately to different exercise replies.

Does anyone have any ideas how I could achieve this?  
Thanks! 😄

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 24, 2022, 3:27pm UTC](https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403/2 "2022-03-24T15:27:36Z")

</div>

I’m a little confused by this example code. In this example `ans` is just a value—3 and `myvar` isn’t passed by the student at all so the checking code can’t know about it. How do you know that `iscorrect` is supposed to compare `ans` with `myvar`?

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [March 24, 2022, 3:56pm UTC](https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403/3 "2022-03-24T15:56:02Z")

</div>

An exercise requires from the student that she does certain things then reports back on the results. A typical exercise might be that the system asks her to create a vector `myvect = collect(7:11)` and reply with the value of the component `myvect[3]`. She enters `reply(9)`, and the program checks whether this is indeed the third component of `myvect`.

I realise that this seems a little contrived in the context of my toy example, but in general, I want to be able to verify the correctness of arbitrary things she has tried out, and this involves evaluating expressions within her `Main` environment.

---

<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:** [March 24, 2022, 4:10pm UTC](https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403/4 "2022-03-24T16:10:05Z")

</div>

There is some reason to be so strict on **how** the things get done, instead of just making a test suit that tests if functions defined by the students always give the right answers given the correct inputs?

This is:

```julia
module Mod

export check_square

function check_square(f)
    for i in -10:10
        # Probably using @testset and @test from module `Test` is better
        @assert f(i) == i ^ 2 
    end

    ... # Other tests for each function.
end

end

using .Mod

f(x) = x^2

check_square(f)

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 24, 2022, 4:19pm UTC](https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403/5 "2022-03-24T16:19:03Z")

</div>

Maybe you need to do more complicated things but for this example it doesn’t look like you need `eval`.

```julia
module Mod

export reply
iscorrect = (ans -> ans == Main.myvar)
reply(ans) = iscorrect(ans)

end # of Mod

using .Mod
myvar = 3
reply(3)

```

---

<div class="post-metadata">

**Author:** ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)\
**Post date:** [March 24, 2022, 4:59pm UTC](https://discourse.julialang.org/t/evaluating-a-local-function-within-the-global-namespace/78403/6 "2022-03-24T16:59:39Z")

</div>

😃 Oh, how glad I am that this forum exists! Gunnar, you’ve saved my life! The students found the bug today and I need to fix it this evening. I would still like to hear if anyone has an answer to my original question, but in the meantime you’ve given me a fix that will get things moving in time for tomorrow. Thanks!
