处理 Void
的最佳方法是什么?函数返回时的类型? http://docs.julialang.org/en/release-0.5/manual/faq/#how-does-null-or-nothingness-work-in-julia 中的建议不工作。
一个 MWE(必须从 REPL 运行,所以 Base.source_dir()
返回 Void
):
julia> isempty(Base.source_dir())
ERROR: MethodError: no method matching start(::Void)
Closest candidates are:
start(::SimpleVector) at essentials.jl:170
start(::Base.MethodList) at reflection.jl:258
start(::IntSet) at intset.jl:184
...
in isempty(::Void) at ./iterator.jl:3
in isempty(::Void) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
julia> isdefined(Base.source_dir())
ERROR: TypeError: isdefined: expected Symbol, got Void
julia> typeof(Base.source_dir()) == Void
true
这是在 Julia 0.5 上。后一个选项有效,但有点难看。
最佳答案
Void
是一个单例——一种只有一个实例的类型。
那一个实例是Void()
也称为 nothing
.
请注意 nothing === Void()
您可以像对待任何其他值一样对待它。
它由一堆函数返回,例如 println
.
您可以检查是否有东西返回 nothing
-- 即 Void
类型的实例.
经过
julia> println()===nothing
true
为了类型稳定性,
方法不应返回
nothing
有些时候,有些时候。在这种情况下,它应该返回
Nullable
,一般来说。
关于Julia 处理 void 返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39885665/