# Getter function of \`Ref\` field not inferred

**URL:** <https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962>\
**Category:** Internals & Design\
**Tags:** bug, inference\
**Created:** [January 23, 2019, 5:46am UTC](https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962 "2019-01-23T05:46:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnliao](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@cnliao](https://discourse.julialang.org/u/cnliao)\
**Post date:** [January 23, 2019, 5:46am UTC](https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962/1 "2019-01-23T05:46:05Z")

</div>

Using `Ref{T}` to get a mutable field in an otherwise immutable struct is causing inference problems (Julia v1.03/v1.1.0), MWE:

```julia
using Test
struct A
    i::Ref{Int}
end
get_i(x) = x.i[]
@inferred get_i(A(2)) # ERROR: return type Int64 does not match inferred return type Any

```

Should I file an issue or is there more idiomatic way to do this?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 23, 2019, 9:06am UTC](https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962/2 "2019-01-23T09:06:49Z")

</div>

The issue is

```julia
julia> isabstracttype(Ref)
true

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

```

If you change your definition to

```julia
struct A
    i::Base.RefValue{Int}
end

```

then you’ll find it’s inferred.

---

<div class="post-metadata">

**Author:** ![cnliao](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@cnliao](https://discourse.julialang.org/u/cnliao)\
**Post date:** [January 23, 2019, 9:59am UTC](https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962/3 "2019-01-23T09:59:12Z")

</div>

Thank you!

Silly me I just took for granted that `Ref{T}` is concrete.

For the curious: try `subtypes(Ref)`, `dump(Ref(1))`, `dump(Ref([1,2],1))` to find out more.

Since `RefValue` is undocumented now I guess breaking it in future 1.x releases is possible?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 23, 2019, 1:15pm UTC](https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962/4 "2019-01-23T13:15:17Z")

</div>

You can always define something like:

> <https://github.com/KristofferC/JuAFEM.jl/blob/a0b0a2e9c225364956d084e9ca89ca5916a6683c/src/utils.jl#L17-L22>

to make your own `RefValue`.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 24, 2019, 9:00pm UTC](https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962/5 "2019-01-24T21:00:44Z")

</div>

Since adding indirection is rather slow anyways, it hasn’t been of much important to optimize this. Doing `mutable struct A` should have much better performance that defining `struct A` and declaring some of the fields to be pointers, even if those pointers are concretely typed (with ScalarWrapper or RefValue or the like)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 24, 2019, 9:18pm UTC](https://discourse.julialang.org/t/getter-function-of-ref-field-not-inferred/19962/6 "2019-01-24T21:18:24Z")

</div>

> [@jameson](#):
>
> Doing `mutable struct A` should have much better performance that defining `struct A` and declaring some of the fields to be pointers

Ok, but there are reasons other than performance to make fields non-mutable. It might just be semantically correct, and having to disable immutability on all fields is unfortunate.
