# Are composite types type-unstable?

**URL:** <https://discourse.julialang.org/t/are-composite-types-type-unstable/20599>\
**Category:** General Usage\
**Tags:** type\
**Created:** [February 8, 2019, 10:27pm UTC](https://discourse.julialang.org/t/are-composite-types-type-unstable/20599 "2019-02-08T22:27:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 8, 2019, 10:27pm UTC](https://discourse.julialang.org/t/are-composite-types-type-unstable/20599/1 "2019-02-08T22:27:34Z")

</div>

I define a composite type with fields of different types:

```julia
struct MyType
    i::Int
    f::Float64
end

```

And then:

```julia
julia> a = MyType(1,1.0)
MyType(1, 1.0)

julia> @code_warntype a.i
Body::Union{Float64, Int64}
18 1 ─ %1 = (Base.getfield)(x, f)::Union{Float64, Int64} │
   └── return %1

```

Does this mean that any code querying any of the fields of a `MyField` instance would be type-unstable?

And if that’s the case, is there a way to avoid it?

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [February 8, 2019, 10:35pm UTC](https://discourse.julialang.org/t/are-composite-types-type-unstable/20599/2 "2019-02-08T22:35:36Z")

</div>

No, composite types are perfectly fine with type inference. What you’re seeing here is misleading because of the way `@code_warntype` interprets expressions.

The macro is showing you the code for `getproperty(::MyType, ::Symbol)` for _unknown_ symbols:

```julia
julia> name = :i

julia> @code_warntype getproperty(a, name)
Body::Union{Float64, Int64}
1 ─ %1 = (Base.getfield)(x, f)::Union{Float64, Int64}
└── return %1

```

Instead, you want the code for a function which uses `getproperty` with a particular symbol which may be const propagated into `getfield`:

```julia
julia> foo(a) = a.i
foo (generic function with 1 method)

julia> @code_warntype foo(a)
Body::Int64
1 ─ %1 = (Base.getfield)(a, :i)::Int64
└── return %1

```

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 8, 2019, 10:49pm UTC](https://discourse.julialang.org/t/are-composite-types-type-unstable/20599/3 "2019-02-08T22:49:00Z")

</div>

Sometimes I find it easier to use the regular (non-macro) versions of the `code_*` methods:

```julia
julia> code_warntype(x -> x.i, (MyType,))
Body::Int64
1 ─ %1 = (Base.getfield)(x, :i)::Int64
└── return %1

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)\
**Post date:** [February 8, 2019, 11:54pm UTC](https://discourse.julialang.org/t/are-composite-types-type-unstable/20599/4 "2019-02-08T23:54:54Z")

</div>

You can also put it through an anonymous function, and it’s the same as first creating a function like `foo`.

```julia
julia> @code_warntype (x->x.i)(a)
Body::Int64
1 ─ %1 = (Base.getfield)(x, :i)::Int64
└── return %1

```
