Julia处理void返回类型

tln*_*agy 7 julia

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
Run Code Online (Sandbox Code Playgroud)

这是Julia 0.5.后一种选择有效,但有点难看.

Lyn*_*ite 10

Void是一个单例 - 只有一个实例的类型.那个例子Void()也被称为nothing.意识到nothing === Void()

您可以像对待任何其他值一样对待它.

它由一堆函数返回,比如println.

您可以检查是否有东西返回nothing- 即类型的实例Void.

通过

julia> println()===nothing
true
Run Code Online (Sandbox Code Playgroud)

为了类型稳定性,一种方法不应该返回nothing某些时间,而某些时候也应该返回.在这种情况下,它Nullable通常应该返回a .