Struct 在Julia中重新分配Union{Nothing,Float64}类型的可变结构字段
假设我有一个简单的可变结构,其中一个字段可以是Float,也可以是NothingStruct 在Julia中重新分配Union{Nothing,Float64}类型的可变结构字段,struct,julia,unions,nothing,Struct,Julia,Unions,Nothing,假设我有一个简单的可变结构,其中一个字段可以是Float,也可以是Nothing mutable struct Foo bar::Union{Nothing, Float64} end foo = Foo(0.42) foo.bar = Nothing 如果我尝试不为其分配任何内容,则会出现以下错误: MethodError: Cannot `convert` an object of type Type{Nothing} to an object of type Float64
mutable struct Foo
bar::Union{Nothing, Float64}
end
foo = Foo(0.42)
foo.bar = Nothing
如果我尝试不为其分配任何内容,则会出现以下错误:
MethodError: Cannot `convert` an object of type Type{Nothing} to an object of type Float64
我应该以不同的方式定义结构吗?
还是有别的办法
提前谢谢你使用
foo.bar=nothing
Nothing
是Nothing
的类型。如果您编写foo.bar=Float64
,也会出现错误。非常感谢Bogumil!