# How to link two Int variables?

**URL:** <https://discourse.julialang.org/t/how-to-link-two-int-variables/92117>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 26, 2022, 12:46am UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117 "2022-12-26T00:46:38Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lighfe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lighfe/32/38831_2.png) [@Lighfe](https://discourse.julialang.org/u/Lighfe)\
**Post date:** [December 26, 2022, 12:46am UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/1 "2022-12-26T00:46:38Z")

</div>

Let’s say I want variable b to update with variable a.

```julia
a = [42]
b = a
a[1] = 69

```

Now b would be 69.

This works but seems rather ugly. Is there a more intuitive way?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 26, 2022, 12:49am UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/2 "2022-12-26T00:49:43Z")

</div>

It’s not exactly clear what you’re looking for. Could you give more context, a more complete example that would illustrate what problem you’re trying to solve.

If you just want a one-element `Vector`, try `Ref`:

[https://docs.julialang.org/en/v1/base/c/#Core.Ref](https://docs.julialang.org/en/v1/base/c/#Core.Ref)

---

<div class="post-metadata">

**Author:** ![Lighfe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lighfe/32/38831_2.png) [@Lighfe](https://discourse.julialang.org/u/Lighfe)\
**Post date:** [December 26, 2022, 1:34am UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/3 "2022-12-26T01:34:11Z")

</div>

```julia
a = 42
b = a
a = 69
b

```

Result: 42  
Wanted: 69

What does Ref() do?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 26, 2022, 1:42am UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/4 "2022-12-26T01:42:44Z")

</div>

Perhaps a demonstration will be clearest:

```julia
julia> a = Ref(42)
Base.RefValue{Int64}(42)

julia> b = a
Base.RefValue{Int64}(42)

julia> a[] = 69
69

julia> a
Base.RefValue{Int64}(69)

julia> b[]
69

julia> b
Base.RefValue{Int64}(69)

julia> a === b
true

```

An important bit to note is that a Ref needs to be deReferenced to get the value using the `b[]` notation.

---

<div class="post-metadata">

**Author:** ![Lighfe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lighfe/32/38831_2.png) [@Lighfe](https://discourse.julialang.org/u/Lighfe)\
**Post date:** [December 26, 2022, 12:39pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/5 "2022-12-26T12:39:28Z")

</div>

Thank you, this helped a lot to understand.

Actually I wanted to implement a dynamic range like this:

```julia
x = Ref(1)
r = x:x+9
x[] = 3

Expected: 3:12

```

Unfortunately RefValues do not work with functions like this. Any ideas?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 26, 2022, 1:11pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/6 "2022-12-26T13:11:18Z")

</div>

You can do that with `Observables.jl`:

```julia
julia> using Observables

julia> x = Observable(1)
Observable(1)

julia> r = map(xx -> xx:xx+9, x)
Observable(1:10)

julia> r
Observable(1:10)

julia> x[] = 3
3

julia> r
Observable(3:12)

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 26, 2022, 1:45pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/7 "2022-12-26T13:45:44Z")

</div>

```julia
julia> x[] = 3
3
julia> r(x)=x:x+9 # r = x->(:)(x,x+9)

julia> r(x[])
3:12

```

```julia
julia> x[]:x[]+9
3:12

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [December 27, 2022, 4:25pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/8 "2022-12-27T16:25:11Z")

</div>

I suggest avoiding such “action at a distance”. It will likely lead to much time debugging unexpected behavior. @rocco_sprmnt21 explicit range construction function is far saner.

> **[Action at a distance (computer programming)](https://en.wikipedia.org/wiki/Action_at_a_distance_(computer_programming))**
>
> Action at a distance is an anti-pattern in computer science in which behavior in one part of a program varies wildly based on difficult or impossible to identify operations in another part of the program.
> The way to avoid the problems associated with action at a distance are a proper design, which avoids global variables and alters data only in a controlled and local manner, or usage of a pure functional programming style with referential transparency.
> The term is based on the concept of act...

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [December 27, 2022, 6:08pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/9 "2022-12-27T18:08:17Z")

</div>

Maybe Pluto.jl would be of interest? It sets up a reactive notebook so that changing a variable reruns all “downstream” cells to update for the new value.

---

<div class="post-metadata">

**Author:** ![Lighfe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lighfe/32/38831_2.png) [@Lighfe](https://discourse.julialang.org/u/Lighfe)\
**Post date:** [December 29, 2022, 10:35am UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/10 "2022-12-29T10:35:59Z")

</div>

Interesting! I saw people using Pluto, but was wondering why. I will have a look at it.  
I wonder if this could lead to issues, once you create the full .jl script and expect it to behave like your notebook did :o

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 29, 2022, 11:30am UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/11 "2022-12-29T11:30:20Z")

</div>

> [@Jeff\_Emanuel](#):
>
> I suggest avoiding such “action at a distance”.

But is this (and your link) a condemnation of reactivity, and of callbacks/listeners/observables/gui-programming? They all rely on “action at a distance”, and it enormously improves the elegance and simplicity of accomplishing this.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 29, 2022, 12:51pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/12 "2022-12-29T12:51:17Z")

</div>

Something that’s not acceptable when writing a module might be acceptable for ad-hoc or exploratory tasks.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 29, 2022, 3:02pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/13 "2022-12-29T15:02:56Z")

</div>

Are you saying that gui creation is inherently ad hoc, or reactivity is only exploratory?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [December 29, 2022, 4:21pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/14 "2022-12-29T16:21:49Z")

</div>

> [@DNF](#):
>
> condemnation of reactivity, and of callbacks/listeners/observables/gui-programming

Maybe. I’ve done GUI programming for over 30 years from Xt/Xm callbacks to newer web reactive frameworks, so I know the loose coupling and extensibility benefits of callbacks and event listeners. But I’ve also see complex GUIs with inter-dependencies where getting the event listeners synchronized correctly can be difficult. An extreme example was where I have worked on a large, complex OO system where once the copious and undisciplined use of event listeners led to such a quagmire that some people suggested eliminating event listeners. Doing that would have been impossible, but it highlights the problems that even standard patterns of “action at a distance” can bring. I’m still fairly new to web reactive frameworks, but their typical usage in populating HTML template seems more structured than ad hoc event listeners. I know they exist, but I just haven’t experienced more general reactivity usage patterns yet. Whether callback, event listener, or reactivity, these patterns require judicious, disciplined usage to avoid unmaintainable code.

That said, if the OP had asked a question about event listeners or observables, then I wouldn’t have got the queasy feeling that I did. I see the stated use case more akin to intentionally exposing an OOP class’s internal state and then having some distant code modifying the internal state.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [December 29, 2022, 4:48pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/15 "2022-12-29T16:48:42Z")

</div>

Here is a good writeup of an example where an observable workflow can get you into trouble: [Makie does not update the plot during an animation - #14 by jules](https://discourse.julialang.org/t/makie-does-not-update-the-plot-during-an-animation/91109/14)

Like with all design patterns, only use them when they make sense and not because you want to use them.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 29, 2022, 7:30pm UTC](https://discourse.julialang.org/t/how-to-link-two-int-variables/92117/16 "2022-12-29T19:30:17Z")

</div>

I have only tried to interpret the intention of the question and find expressions that are closest to the expectations of the asker.  
I didn’t have the intention (nor the sufficient general knowledge) to endorse the style or even to suggest alternative routes.

> [@Lighfe](#):
>
> Actually I wanted to implement a dynamic range like this:
> 
> ```julia
> x = Ref(1)
> r = x:x+9
> x[] = 3
> 
> Expected: 3:12
> 
> ```
> 
> Unfortunately RefValues do not work with functions like this. Any ideas?

for the rest I try to follow the discussion that ensued and maybe learn some new (for me) concept.
