# Ref not change

**URL:** https://discourse.julialang.org/t/ref-not-change/33503
**Category:** New to Julia
**Tags:** question
**Created:** [January 17, 2020, 7:14pm UTC](https://discourse.julialang.org/t/ref-not-change/33503 "2020-01-17T19:14:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [January 17, 2020, 7:14pm UTC](https://discourse.julialang.org/t/ref-not-change/33503/1 "2020-01-17T19:14:39Z")

</div>

Hello!

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

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [January 17, 2020, 7:35pm UTC](https://discourse.julialang.org/t/ref-not-change/33503/2 "2020-01-17T19:35:55Z")

</div>

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

```julia
x = 10

```

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

```julia
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)`.

```julia
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.

```julia
println(ax[])

```

is then ‘equivalent’ to

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

```

and

```julia
println(10)

```

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [January 17, 2020, 7:47pm UTC](https://discourse.julialang.org/t/ref-not-change/33503/3 "2020-01-17T19:47:57Z")

</div>

thank you  
how to get value 90 from reference?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [January 17, 2020, 7:51pm UTC](https://discourse.julialang.org/t/ref-not-change/33503/4 "2020-01-17T19:51:27Z")

</div>

Try this:

```julia
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`.

---

<div class="post-metadata">

### Author: ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)
#### Post date: [January 17, 2020, 8:03pm UTC](https://discourse.julialang.org/t/ref-not-change/33503/5 "2020-01-17T20:03:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)
#### Post date: [January 17, 2020, 9:09pm UTC](https://discourse.julialang.org/t/ref-not-change/33503/6 "2020-01-17T21:09:40Z")

</div>

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

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

```

`90`

or:

```julia
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
julia> x::Int = 10 
ERROR: syntax: type declarations on global variables are not yet supported
....

```

---

<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: [January 17, 2020, 9:14pm UTC](https://discourse.julialang.org/t/ref-not-change/33503/7 "2020-01-17T21:14:32Z")

</div>

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.
