Julia 处理 void 返回类型

标签 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

这是在 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/

相关文章:

Julia 中的 Python None 和 numpy 命令

julia - 如何在 Juliabox 上写表格?

julia - 在 Julia 中排序的两个数组的组合

julia - 没有返回的中断函数

performance - Julia (Julia-lang) 性能与 Fortran 和 Python 的比较

dictionary - 生成包含范围的字典的所有组合

julia - 如何在 Julia 中使用未注册的包?

latex - 剧情+ Julia + latex

file - 在同一文件上写入并行模拟

python - 有没有办法将 Julia、R 和 python 添加到单个文本文件(例如 R markdown)或可以作为文本文件进行操作的笔记本?