# TypeError for setfield! using Unions

**URL:** <https://discourse.julialang.org/t/typeerror-for-setfield-using-unions/20850>\
**Category:** New to Julia\
**Created:** [February 16, 2019, 6:05am UTC](https://discourse.julialang.org/t/typeerror-for-setfield-using-unions/20850 "2019-02-16T06:05:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crlaugh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crlaugh/32/1817_2.png) [@crlaugh](https://discourse.julialang.org/u/crlaugh)\
**Post date:** [February 16, 2019, 6:05am UTC](https://discourse.julialang.org/t/typeerror-for-setfield-using-unions/20850/1 "2019-02-16T06:05:50Z")

</div>

I am trying to set a field of a mutable composite type that happens to be an array of type Union{Float64,Nothing}, but am unable to do this successfully, and would like to understand why. As a MWE, I have

```julia
mutable struct TestFunction
    x::Union{Float64, Nothing}
    y::Array{Union{Float64, Nothing},1}
    function TestFunction(a::String, b::Float64)
        println("Hello, world!: $a $b")
        return new(nothing, [nothing])
    end
end

testStruct = TestFunction("hello", 3.0)
testVar = collect(0:0.1:2)
setfield!(testStruct, :y, testVar)

```

When I run this code, I get the following error at the last line:

```julia
ERROR: TypeError: in setfield!, expected Array{Union{Nothing, Float64},1}, got Array{Float64,1}

```

However, if I instead do

```julia
testStruct.y = testVar

```

it works just fine. It seems like both of these should work, but I don’t quite understand why the former doesn’t. Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 16, 2019, 9:23am UTC](https://discourse.julialang.org/t/typeerror-for-setfield-using-unions/20850/2 "2019-02-16T09:23:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crlaugh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crlaugh/32/1817_2.png) [@crlaugh](https://discourse.julialang.org/u/crlaugh)\
**Post date:** [February 16, 2019, 5:16pm UTC](https://discourse.julialang.org/t/typeerror-for-setfield-using-unions/20850/3 "2019-02-16T17:16:08Z")

</div>

Thanks - I see how the automatic conversion doesn’t happen, and that convert should take care of it. I tried modifying the last line so that it now reads

```julia
setfield!(testStruct, :y, convert(fieldtype(testStruct, :y), testVar))

```

This now produces a new, improved error:

```julia
ERROR: TypeError: in fieldtype, expected DataType, got TestFunction

```

I think that this is because I am using an inner constructor function. I can’t seem to get the type of the struct - suggestions?

---

<div class="post-metadata">

**Author:** ![crlaugh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crlaugh/32/1817_2.png) [@crlaugh](https://discourse.julialang.org/u/crlaugh)\
**Post date:** [February 17, 2019, 4:30am UTC](https://discourse.julialang.org/t/typeerror-for-setfield-using-unions/20850/4 "2019-02-17T04:30:04Z")

</div>

Ah, I got it. I needed to pass in the type of testStruct, so the last line needed to be

```julia
setfield!(testStruct, :y, convert(fieldtype(typeof(testStruct), :y), testVar))

```

Thanks again for your help.
