# Inferring the type of a field of a type

**URL:** <https://discourse.julialang.org/t/inferring-the-type-of-a-field-of-a-type/3312>\
**Category:** New to Julia\
**Created:** [April 21, 2017, 1:58am UTC](https://discourse.julialang.org/t/inferring-the-type-of-a-field-of-a-type/3312 "2017-04-21T01:58:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabgrei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabgrei/32/4214_2.png) [@fabgrei](https://discourse.julialang.org/u/fabgrei)\
**Post date:** [April 21, 2017, 1:58am UTC](https://discourse.julialang.org/t/inferring-the-type-of-a-field-of-a-type/3312/1 "2017-04-21T01:58:19Z")

</div>

I noticed that Julia cannot infer the type of fields in user-defined types. Is that a bug or a feature?

```julia
struct MyType1{T<:Real}
    x::Vector{T}
end

function foo{T<:Real}(y::MyType1{T})
    a = y.x
end

yy = MyType1(zeros(5))

```

Then the type of `yy.x` cannot be inferred.

```julia
julia> @code_warntype(foo(yy))
Variables:
  #self#::#foo
  y::MyType1{Float64}
  a::ANY

Body:
  begin 
      SSAValue(0) = (Core.getfield)(y::MyType1{Float64}, :x)::Array{Float64,1}
      return SSAValue(0)
  end::Array{Float64,1}

```

```julia
julia> versioninfo()
Julia Version 0.6.0-pre.beta.68
Commit e5e365c* (2017-04-07 18:50 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin15.5.0)
  CPU: Intel(R) Core(TM)2 Duo CPU P8600 @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Penryn)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, penryn)

```

the same thing happens on latest master. It worked well on 0.5.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 21, 2017, 2:07am UTC](https://discourse.julialang.org/t/inferring-the-type-of-a-field-of-a-type/3312/2 "2017-04-21T02:07:04Z")

</div>

> [@fabgrei](#):
>
> `a::ANY`

That’s weird, it shows `a::Array{Float64,1}` in Julia 0.5, and in 0.6 I get `a::ANY` like you but the return type of the function is correctly inferred as `Array{Float64,1}`.

I think this may be just a bug in `@code_warntype`, and inference is actually working. If you actually do something with the `a` variable, it works fine:

```julia
julia> function bar{T<:Real}(y::MyType1{T})
                  a = y.x
                  return a + 1
       end
bar (generic function with 1 method)

julia> @code_warntype bar(yy)
Variables:
  #self#::#bar
  y::MyType1{Float64}
  a::Array{Float64,1}

Body:
  begin 
      a::Array{Float64,1} = (Core.getfield)(y::MyType1{Float64}, :x)::Array{Float64,1} # line 3:
      return $(Expr(:invoke, MethodInstance for +(::Array{Float64,1}, ::Int64), :(Main.+), :(a), 1))
  end::Array{Float64,1}

```

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 21, 2017, 2:17am UTC](https://discourse.julialang.org/t/inferring-the-type-of-a-field-of-a-type/3312/3 "2017-04-21T02:17:20Z")

</div>

`a` is unused so there is no need to infer it. It’s already been optimized out.

[https://github.com/JuliaLang/julia/issues/20801](https://github.com/JuliaLang/julia/issues/20801)
