# Help me with @eval

**URL:** <https://discourse.julialang.org/t/help-me-with-eval/59341>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 15, 2021, 1:41pm UTC](https://discourse.julialang.org/t/help-me-with-eval/59341 "2021-04-15T13:41:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [April 15, 2021, 1:41pm UTC](https://discourse.julialang.org/t/help-me-with-eval/59341/1 "2021-04-15T13:41:34Z")

</div>

Hey all 🙂

Below I wrote a minimal working example of what I’m trying to do. But I can’t get it to work. I’m doing something wrong.

```julia
julia> mutable struct Test
           field_1        
           field_2        
           field_3        
       end

julia> function test_run()
           test = Test(0,0,0)
           for field_number in 1:3
               @eval test.$(Symbol(:field_, field_number)) = $field_number
           end
           test
       end
test_run (generic function with 1 method)

julia> test_run()
ERROR: UndefVarError: test not defined
Stacktrace:
 [1] top-level scope
   @ none:1

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 15, 2021, 1:45pm UTC](https://discourse.julialang.org/t/help-me-with-eval/59341/2 "2021-04-15T13:45:30Z")

</div>

Eval works in global scope, even when called inside a function or some other local scope. That’s why it cannot see the variable `test`.

In your example you could just remove the function and just run the loop? In general, avoid using eval in functions.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [April 15, 2021, 1:51pm UTC](https://discourse.julialang.org/t/help-me-with-eval/59341/3 "2021-04-15T13:51:06Z")

</div>

Is there something that evals code in local/ function scope?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 15, 2021, 1:55pm UTC](https://discourse.julialang.org/t/help-me-with-eval/59341/4 "2021-04-15T13:55:02Z")

</div>

No, this would be fundamentally incompatible with how Julia’s JIT compilation works. In this case you can just use `setproperty!` though, which should be much more efficient anyways.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [April 15, 2021, 1:55pm UTC](https://discourse.julialang.org/t/help-me-with-eval/59341/5 "2021-04-15T13:55:58Z")

</div>

You don’t need `@eval`, you can assign the value to a field with `setproprerty!`:

```julia
julia> mutable struct Test
                  field_1
                  field_2
                  field_3
          end

julia> function test_run()
           test = Test(0,0,0)
           for field_number in 1:3
               setproperty!(test, Symbol(:field_, field_number), field_number)
           end
           test
       end
test_run (generic function with 1 method)

julia> test_run()
Test(1, 2, 3)

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [April 15, 2021, 2:04pm UTC](https://discourse.julialang.org/t/help-me-with-eval/59341/6 "2021-04-15T14:04:56Z")

</div>

Thanks! I learnt a lot 😍
