# Understanding type Ref{T}

**URL:** <https://discourse.julialang.org/t/understanding-type-ref-t/107711>\
**Category:** New to Julia\
**Created:** [December 16, 2023, 3:01pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711 "2023-12-16T15:01:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hlbnet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hlbnet/32/204549_2.png) [@hlbnet](https://discourse.julialang.org/u/hlbnet)\
**Post date:** [December 16, 2023, 3:01pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/1 "2023-12-16T15:01:33Z")

</div>

Hello,

I am reading the documentation of the Ref type template: [C Interface · The Julia Language](https://docs.julialang.org/en/v1/base/c/#Core.Ref)

Unfortunately, it doesn’t make sense to me. There are a few examples of use of the Ref type, but I hardly see any value of the use of Ref from these examples.

Let’s look at this one:

```julia
julia> isa.(Ref([1,2,3]), [Array, Dict, Int]) # Treat reference values as scalar during broadcasting

```

I tested it, and then I tested the same statement without the Ref, like that:

```julia
isa.([1,2,3], [Array, Dict, Int])

```

And the result seems to be exactly the same.

Can someone explain what is the purpose of the type Ref and provide an example of use that brings some value compared to not using it ?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 16, 2023, 3:16pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/2 "2023-12-16T15:16:51Z")

</div>

> [@hlbnet](#):
>
> And the result seems to be exactly the same.

Are you sure?

```julia
julia> isa.(Ref([1,2,3]), [Array, Dict, Int])
3-element BitVector:
 1
 0
 0

julia> isa.([1,2,3], [Array, Dict, Int])
3-element BitVector:
 0
 0
 1

```

The first one tests whether `[1,2,3] isa Array`, then `[1,2,3] isa Dict` and finally `[1,2,3] isa Int`.

The latter tests whether `1 isa Array`, then `2 isa Array`, then `3 isa Int`.

---

<div class="post-metadata">

**Author:** ![hlbnet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hlbnet/32/204549_2.png) [@hlbnet](https://discourse.julialang.org/u/hlbnet)\
**Post date:** [December 16, 2023, 3:23pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/3 "2023-12-16T15:23:53Z")

</div>

Oh, I did not watch carefully the results. Thanks for pointing me on this.

So, finally, the Ref type is used to prevent broadcasting from seing and array as an array, but consider it as a single value.

Thanks for this.

Is this the only purpose of Ref, or are there other usages ?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 16, 2023, 3:41pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/4 "2023-12-16T15:41:28Z")

</div>

It’s a general single-value container - you can also use it for C interop if your C function expects a pointer.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 16, 2023, 6:08pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/5 "2023-12-16T18:08:56Z")

</div>

You could also use a `Tuple` instead of a `Ref`.

```julia-repl
julia> isa.(Ref([1,2,3]), [Array, Dict, Int])
3-element BitVector:
 1
 0
 0

julia> isa.(([1,2,3],), [Array, Dict, Int])
3-element BitVector:
 1
 0
 0

julia> @time isa.(([1,2,3],), (Array, Dict, Int))
  0.000007 seconds (1 allocation: 80 bytes)
(true, false, false)

```

Using a tuple for the types might end up saving some allocations as well.

---

<div class="post-metadata">

**Author:** ![hlbnet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hlbnet/32/204549_2.png) [@hlbnet](https://discourse.julialang.org/u/hlbnet)\
**Post date:** [December 16, 2023, 6:17pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/6 "2023-12-16T18:17:47Z")

</div>

Yes, Ref doesn’t seem necessary for this purpose.

From answers so far, I make the hypothesis that Ref is a type dedicated to communication with C and has no practical purpose in pure Julia code.

I hope I am right …

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [December 16, 2023, 6:45pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/7 "2023-12-16T18:45:45Z")

</div>

> [@hlbnet](#):
>
> From answers so far, I make the hypothesis that Ref is a type dedicated to communication with C and has no practical purpose in pure Julia code.

If you need a container with just a single value, e.g. for keeping track of something global in a deep recursion, or some such thing, `Ref` can be useful. But you may of course use a `Vector` of length 1 instead.

---

<div class="post-metadata">

**Author:** ![hlbnet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hlbnet/32/204549_2.png) [@hlbnet](https://discourse.julialang.org/u/hlbnet)\
**Post date:** [December 16, 2023, 6:50pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/8 "2023-12-16T18:50:25Z")

</div>

Thanks for your answer @sgaure

Sorry for asking, but can you put your idea into a short snippet of Julia code so that I can understand the idea ?

Sorry again for being slow at understanding, I am very new to Julia.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 16, 2023, 9:39pm UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/9 "2023-12-16T21:39:12Z")

</div>

> [@hlbnet](#):
>
> From answers so far, I make the hypothesis that Ref is a type dedicated to communication with C and has no practical purpose in pure Julia code.

That’s a specific case actually. `Ref` is generally used to let bindings (not sure if this is the right word, I mean variables, fields, elements, etc) share data, to put it colloquially. More formally, it is a mutable container; mutable types are already mutable, so it’s mostly used on immutable types. Despite the name, mutability doesn’t have a monopoly on changing data; reassigning variables e.g. `x+=1` does not mutate. However, if you want multiple bindings to see a change at once, you shouldn’t reassign all of them, but provide them with a mutable object, so `Ref` can help:

```julia
julia> x=y=z=1
1

julia> x+=1 # reassigning one variable...
2

julia> x,y,z # ...doesn't reassign the others.
(2, 1, 1)

julia> x=y=z=Ref(1)
Base.RefValue{Int64}(1)

julia> x[] += 1 # mutate the `Ref` by reassigning its element
2

julia> x,y,z
(Base.RefValue{Int64}(2), Base.RefValue{Int64}(2), Base.RefValue{Int64}(2))

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 17, 2023, 2:13am UTC](https://discourse.julialang.org/t/understanding-type-ref-t/107711/10 "2023-12-17T02:13:07Z")

</div>

Another way to think of `Ref` is as a 0-dimensional array. It is an array like structure with exactly one element in it. `Ref` is actually an abstract type, but when used as a constructor it produces a `Base.RefValue`.

You can otherwise think of it as a pointer to a single element of a specified type.

```julia-repl
julia> r = Ref(5)
Base.RefValue{Int64}(5)

julia> r[]
5

julia> ndims(r)
0

julia> size(r)
()

julia> eltype(r)
Int64

julia> r[] = 3
3

julia> r[] = 3.5
ERROR: InexactError: Int64(3.5)

```
