What is the best method for handling a Void
type when it is returned by a function? The suggestions in http://docs.julialang.org/en/release-0.5/manual/faq/#how-does-null-or-nothingness-work-in-julia don't work.
A MWE (must be run from the REPL so Base.source_dir()
returns 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
This is on Julia 0.5. The latter option works, but it's a bit ugly.
Void
is a singleton -- a type with exactly one instance.
That one instance is Void()
also called nothing
.
Be aware that nothing === Void()
You can treat it just like any other value.
It is returned by a bunch of functions, like println
.
You can check if something has returned nothing
-- ie and instance of type Void
.
By
julia> println()===nothing
true
For the sake of type-stability,
a method should not return nothing
some of the time, and something some of the time.
in those case it should instead return a Nullable
,
generally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With