# Eval symbol on LHS for assignment

**URL:** <https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223>\
**Category:** General Usage\
**Created:** [August 11, 2018, 3:49am UTC](https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223 "2018-08-11T03:49:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [August 11, 2018, 3:49am UTC](https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223/1 "2018-08-11T03:49:45Z")

</div>

the following code does not work, but can julia allow me to do this?

```julia
julia> names = [:x, :y];

julia> eval(names[1])= 42 ## should be the same as x=42

julia> x
42

```

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [August 11, 2018, 3:59am UTC](https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223/2 "2018-08-11T03:59:53Z")

</div>

Try,

```julia
julia> names=[:x,:y]
2-element Array{Symbol,1}:
 :x
 :y

julia> @eval $(names[1]) = 42
42

julia> x
42

```

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [August 11, 2018, 3:56pm UTC](https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223/3 "2018-08-11T15:56:46Z")

</div>

```julia
names= [:x, :y]

function work( values::Vector{Int} )
    println("using $(values), but the eval inside work() fails")
    @eval $(names[1])= values[1]
    println("or this fails, too")
    for i=1:length(values);	@eval $(names[i])= values[i]; end
    println("now we can work with x=$x and y=$y")
end

vals= [1, 2]
@eval $(names[1])= vals[1]
println("in main, this worked with x=$x")

work( vals )

```

closely related. how could this be made to work?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [August 11, 2018, 4:59pm UTC](https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223/4 "2018-08-11T16:59:28Z")

</div>

There’s no direct way to do this with `eval` since that only evaluates things in the global scope, not the function scope. Is there any reason,

```julia
function work(x,y)
  # your code here
end

vals = [1,2]
work(vals...)

```

wouldn’t work?

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [August 11, 2018, 5:10pm UTC](https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223/5 "2018-08-11T17:10:06Z")

</div>

no, it would not just work…it is much better! argggh!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 11, 2018, 5:22pm UTC](https://discourse.julialang.org/t/eval-symbol-on-lhs-for-assignment/13223/6 "2018-08-11T17:22:47Z")

</div>

The only restriction is that you cannot get and set a local variable by runtime name. Everything else is fine. If you want to do just this, then no, don’t think about it. Otherwise, you should specify what does “work” mean, or what you want. Just getting the code to not raise an error is trivial but it may or may not do what you want.

It’s also much better to **NOT** use `@eval` to learn about `eval` or `macro`. It blends multiple scope and evaluaiton rules together making it very confusing before you know all of them well already. You should use the `eval` function first before you know all the rules.
