# How to overload \`ConstructionBase.setproperties\` to change struct properties?

**URL:** <https://discourse.julialang.org/t/how-to-overload-constructionbase-setproperties-to-change-struct-properties/83686>\
**Category:** General Usage\
**Created:** [July 3, 2022, 4:52am UTC](https://discourse.julialang.org/t/how-to-overload-constructionbase-setproperties-to-change-struct-properties/83686 "2022-07-03T04:52:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vkv](https://avatars.discourse-cdn.com/v4/letter/v/d9b06d/32.png) [@vkv](https://discourse.julialang.org/u/vkv)\
**Post date:** [July 3, 2022, 4:52am UTC](https://discourse.julialang.org/t/how-to-overload-constructionbase-setproperties-to-change-struct-properties/83686/1 "2022-07-03T04:52:01Z")

</div>

I want to change the value of certain properties in a struct. But Setfield is not working.

MWE:

```julia
using Setfield

struct C{S,T, K}
    i::T
    j::Bool
end

c = C{Float64, Int, BitArray}(1.0,true)

@set! c.i = 2

```

`ERROR: MethodError: no method matching C(::Int64, ::Bool)`

How to overload `ConstructionBase.setproperties` to achieve the above?

---

<div class="post-metadata">

**Author:** ![vkv](https://avatars.discourse-cdn.com/v4/letter/v/d9b06d/32.png) [@vkv](https://discourse.julialang.org/u/vkv)\
**Post date:** [July 3, 2022, 5:09am UTC](https://discourse.julialang.org/t/how-to-overload-constructionbase-setproperties-to-change-struct-properties/83686/2 "2022-07-03T05:09:22Z")

</div>

The actual problem:

```julia
using Turing, Setfield

alg = NUTS(0.65)
@set! alg.max_depth = 4

ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Symbol
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:205
  Symbol(::Any...) at strings/basic.jl:229 

```

So overloading `ConstructionBase.setproperties` can do what I want. But I was not able get it right.

---

<div class="post-metadata">

**Author:** ![vkv](https://avatars.discourse-cdn.com/v4/letter/v/d9b06d/32.png) [@vkv](https://discourse.julialang.org/u/vkv)\
**Post date:** [July 3, 2022, 7:03pm UTC](https://discourse.julialang.org/t/how-to-overload-constructionbase-setproperties-to-change-struct-properties/83686/3 "2022-07-03T19:03:22Z")

</div>

Solution:

@using Setfield, ConstructionBase, Turing

alg = NUTS(0.65)

ConstructionBase.constructorof(::Type{NUTS{A, B, C}}) where {A, B, C} = (x…) → NUTS{A, B, C}(x…)

@set! alg.max\_depth = 4
