# Setfield fails for structs with "label"-like type annotations

**URL:** <https://discourse.julialang.org/t/setfield-fails-for-structs-with-label-like-type-annotations/65324>\
**Category:** General Usage\
**Created:** [July 26, 2021, 5:43pm UTC](https://discourse.julialang.org/t/setfield-fails-for-structs-with-label-like-type-annotations/65324 "2021-07-26T17:43:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 26, 2021, 5:43pm UTC](https://discourse.julialang.org/t/setfield-fails-for-structs-with-label-like-type-annotations/65324/1 "2021-07-26T17:43:36Z")

</div>

Is this expected to fail?

```julia
julia> using Setfield

julia> struct C{S,T}
         i::T
       end

julia> c = C{Float64,Int}(1)
C{Float64, Int64}(1)

julia> @set! c.i = 2
ERROR: MethodError: no method matching C(::Int64)

```

In my practical case, the `S` field would be only for managing dispatch, but the result is the same.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [July 26, 2021, 6:13pm UTC](https://discourse.julialang.org/t/setfield-fails-for-structs-with-label-like-type-annotations/65324/2 "2021-07-26T18:13:32Z")

</div>

This behaviour is expected, but there are multiple ways to get what you want. One way is

```julia
struct C{S,T}
     s::S
     i::T
end

```

I like this design if `S` is an empty “trait type” e.g. `sizeof(S) = 0`. It has the advantage, that you can do things like `@set c.s = S2()`.  
If that is not an option, overload `ConstructionBase.constructorof` or `ConstructionBase.setproperties` for more complicated cases.

[https://github.com/JuliaObjects/ConstructionBase.jl](https://github.com/JuliaObjects/ConstructionBase.jl)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 26, 2021, 6:27pm UTC](https://discourse.julialang.org/t/setfield-fails-for-structs-with-label-like-type-annotations/65324/3 "2021-07-26T18:27:37Z")

</div>

> [@jw3126](#):
>
> It has the advantage, that you can do things like `@set c.s = S2()` .

I didn’t think of that, but actually that might be useful. Thanks.
