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
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:
ERROR: TypeError: in setfield!, expected Array{Union{Nothing, Float64},1}, got Array{Float64,1}
However, if I instead do
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.