# Type instabilities when dealing with structs and Ref

**URL:** <https://discourse.julialang.org/t/type-instabilities-when-dealing-with-structs-and-ref/115122>\
**Category:** Performance\
**Tags:** struct, code\_warntype, type-stability\
**Created:** [June 3, 2024, 2:07pm UTC](https://discourse.julialang.org/t/type-instabilities-when-dealing-with-structs-and-ref/115122 "2024-06-03T14:07:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [June 3, 2024, 2:07pm UTC](https://discourse.julialang.org/t/type-instabilities-when-dealing-with-structs-and-ref/115122/1 "2024-06-03T14:07:30Z")

</div>

Hello,

I’m facing type instabilities with a struct containing a Ref value. Here I show a minimal example

```julia
struct Prova{T1<:Integer}
    a::Ref{T1}
    function Prova(a::T1) where {T1}
        new{T1}(Ref(a))
    end
end

function function_test(p::Prova)
    c = p.a[]
    if !iszero(c)
        return c
    end
    return 46
end

p = Prova(42)

```

```julia
Prova{Int64}(Base.RefValue{Int64}(42))

```

But if I test the type instabilities I get

```julia
@code_warntype function_test(p)

```

```julia
MethodInstance for function_test(::Prova{Int64})
  from function_test(p::Prova) @ Main Untitled-1:111
Arguments
  #self#::Core.Const(function_test)
  p::Prova{Int64}
Locals
  c::Any
Body::Any
1 ─ %1 = Base.getproperty(p, :a)::Ref{Int64}
│ (c = Base.getindex(%1))
│ %3 = Main.iszero(c)::Any
│ %4 = !%3::Any
└── goto #3 if not %4
2 ─ return c
3 ─ return 46

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 3, 2024, 2:14pm UTC](https://discourse.julialang.org/t/type-instabilities-when-dealing-with-structs-and-ref/115122/2 "2024-06-03T14:14:51Z")

</div>

`Ref` is an abstract type. You need `Base.RefValue` in your struct.

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

```

---

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [June 3, 2024, 3:25pm UTC](https://discourse.julialang.org/t/type-instabilities-when-dealing-with-structs-and-ref/115122/3 "2024-06-03T15:25:31Z")

</div>

But how to solve it then?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 3, 2024, 3:30pm UTC](https://discourse.julialang.org/t/type-instabilities-when-dealing-with-structs-and-ref/115122/4 "2024-06-03T15:30:04Z")

</div>

```julia
struct Prova{T1<:Integer}
    a::Base.RefValue{T1}
    function Prova(a::T1) where {T1}
        new{T1}(Ref(a))
    end
end

```

---

<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:** [June 3, 2024, 9:35pm UTC](https://discourse.julialang.org/t/type-instabilities-when-dealing-with-structs-and-ref/115122/5 "2024-06-03T21:35:07Z")

</div>

Perhaps a nicer approach:

```julia-repl
julia> struct S{T<:Ref{<:Integer}}
           a::T
       end

julia> f(x) = S(Ref(x))
f (generic function with 1 method)

julia> f(42)
S{Base.RefValue{Int64}}(Base.RefValue{Int64}(42))

```
