struct - 在 Julia 中重新分配 Union{Nothing, Float64} 类型的可变结构字段
问题描述
假设我有一个简单的可变结构,其字段可以是浮点数或空值
mutable struct Foo
bar::Union{Nothing, Float64}
end
foo = Foo(0.42)
foo.bar = Nothing
如果我尝试将 Nothing 分配给它,我会收到此错误:
MethodError: Cannot `convert` an object of type Type{Nothing} to an object of type Float64
我应该以不同的方式定义我的结构吗?或者还有其他方法可以解决这个问题吗?
先感谢您
解决方案
使用foo.bar = nothing
. Nothing
是 的类型nothing
。
推荐阅读
- scala - 如何运行示例 Scala vertx 项目
- reactjs - 为什么我得到 Erroe:期望一个赋值或函数调用,而是看到一个表达式 no-unused-expressions
- c# - SignInManager 引发无效的对象名称 AspNetUserClaims 异常
- sql - 用于循环日期和汇总数据的 SQL
- java - 我的应用程序中的 ListenOrderService(通知)中的错误
- java - 如何输出到 textField?
- mysql - 如何更改mysql8.0端口
- android-studio - Android Studio 和“错误:找不到符号类”
- javascript - 查找最短持续时间
- python - 根据条件在某些组上基于转换创建新的 col