# Ref not change

Hello!

``````x = 10
ax = Ref(x)
x = 90
println(ax[]) # why 10 ?
``````

Here’s a step by step explanation of what your program does:

``````x = 10
``````

This binds the value `10` to a variable `x`. Using `x` or the literal `10` should now be interchangeable.

``````ax = Ref(x)
``````

This plops the value of `x` into a `Ref` and names that object `ax`. What you did is fully equivalent to `ax = Ref(10)`.

``````x = 90
``````

This just rebinds the variable `x` to point to the value `90` instead of `10` for future uses. This rebinding is not retroactive.

``````println(ax[])
``````

is then ‘equivalent’ to

``````println(Ref(10)[]
``````

and

``````println(10)
``````
2 Likes

thank you
how to get value 90 from reference?

Try this:

``````x = 10
ax = Ref(x)
ax[] = 90
println(ax[])
``````

the line `ax[] = 90` says mutate the `Ref` so that it not stores `90` instead of `10`.

1 Like

but, how to get the correct value from ref when changing x ?

why not taking a composite type? This is typesafe and needs no explicit pointers:

``````x = [10]
ax = x
x[1] = 90
ax[1]
``````

`90`

or:

``````mutable struct mytype
y::Int
end
x = mytype(10)
ax = x
x.y = 90
ax.y
``````

`90`

Why is this so? You could in your second assignment do `x = 90.0`. Then x would change to a `Float64` and ax would be in trouble. try like in C `int x 10`:

``````julia> x::Int = 10
ERROR: syntax: type declarations on global variables are not yet supported
....
``````
1 Like

It is strictly not allowed for assigning to a simple local variable to have such a effect so what you are asking for is impossible.