# Can the @unpack\_STRUCT macro in Parameters.jl be type stable?

**URL:** https://discourse.julialang.org/t/can-the-unpack-struct-macro-in-parameters-jl-be-type-stable/7873
**Category:** General Usage
**Created:** [December 20, 2017, 10:19am UTC](https://discourse.julialang.org/t/can-the-unpack-struct-macro-in-parameters-jl-be-type-stable/7873 "2017-12-20T10:19:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![NickNack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicknack/32/1792_2.png) [@NickNack](https://discourse.julialang.org/u/NickNack)
#### Post date: [December 20, 2017, 10:19am UTC](https://discourse.julialang.org/t/can-the-unpack-struct-macro-in-parameters-jl-be-type-stable/7873/1 "2017-12-20T10:19:33Z")

</div>

I want to use the `@unpack_STRUCT` macro in Parameters.jl to unpack all fields of a mutable struct, but I need it to be type stable. Here is an example:

```julia
using Parameters, BenchmarkTools

@with_kw mutable struct S @deftype Float64
    a;b;c;d
end

function f!(s::S)
    @unpack a,b,c = s
    d = a*b*c
    @pack s = d
    return s
end

function g!(s::S)
    @unpack_S s
    d = a*b*c
    @pack_S s
end

```

In this example, `f!(s)` is type stable but `g!(s)` is not.

```julia
julia> s = S(2,3,4,1); @btime f!(s);
  16.682 ns (0 allocations: 0 bytes)

julia> s = S(2,3,4,1); @btime g!(s);
  4.641 μs (54 allocations: 3.13 KiB)

julia> @code_warntype g!(s)
Variables:
  #self# <optimized out>
  s@_2::S
  a::Float64
  b::Float64
  c::Float64
  d::Float64
  s@_7::Any

Body:
  begin
      s@_7::Any = s@_2::S
      a::Float64 = (Core.getfield)(s@_7::S, :a)::Float64
      b::Float64 = (Core.getfield)(s@_7::S, :b)::Float64
      c::Float64 = (Core.getfield)(s@_7::S, :c)::Float64
      d::Float64 = (Core.getfield)(s@_7::S, :d)::Float64 # line 15:
      d::Float64 = (Base.mul_float)((Base.mul_float)(a::Float64, b::Float64)::Float64, c::Float64)::Float64 # line 16:
      SSAValue(0) = $(Expr(:invoke, MethodInstance for (::Parameters.#kw##reconstruct)(::Array{Any,1}, ::Parameters.#reconstruct, ::S), :($(QuoteNode(Parameters.#reconstruct))), :($(Expr(:invoke, MethodInstance for vector_any(::Any, ::Vararg{Any,N} where N), :(Base.vector_any), :(:a), :(a), :(:b), :(b), :(:c), :(c), :(:d), :(d)))), :($(QuoteNode(Parameters.reconstruct))), :(s@_7::S)))
      s@_7::Any = SSAValue(0)
      return SSAValue(0)
  end::Any

```

Is there a way to make `g!(s)` type stable or do I need to list individual fields as I did in `f!(s)`?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 20, 2017, 10:25am UTC](https://discourse.julialang.org/t/can-the-unpack-struct-macro-in-parameters-jl-be-type-stable/7873/2 "2017-12-20T10:25:14Z")

</div>

It’s the `@pack_S` which is not type stable. I’ll look into it.

---

<div class="post-metadata">

### Author: ![NickNack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicknack/32/1792_2.png) [@NickNack](https://discourse.julialang.org/u/NickNack)
#### Post date: [December 20, 2017, 10:45am UTC](https://discourse.julialang.org/t/can-the-unpack-struct-macro-in-parameters-jl-be-type-stable/7873/3 "2017-12-20T10:45:37Z")

</div>

Yeah, I goofed on the title there. Thanks, I’ll follow any progress in the repo!

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 20, 2017, 11:12am UTC](https://discourse.julialang.org/t/can-the-unpack-struct-macro-in-parameters-jl-be-type-stable/7873/4 "2017-12-20T11:12:27Z")

</div>

It’s fixed on the latest master.
