Search code examples
metaprogrammingjuliaassignment-operator

What is the correct syntax in metaprogramming assignment in Julia?


I want x to take the value 4; why doesn't this work? What would the correct syntax be?

x=3
y=5

z=[:x; :y]

:(z[1])=4

Solution

  • The equivalent of &x in C++ in Julia, is to use a Ref.

    x = Ref(1)
    x[]     # get value of x, it's 1
    x[] = 2 # set value of x to 2
    

    What you want to do is

    x = Ref(3)
    y = Ref(5)
    z = [x, y]
    z[1][] = 4
    

    For more information, see the section on Ref in the documentation.