Julia, referring to the return type

What is the best processing method of type Void when it is returned by a function? Suggestions http://docs.julialang.org/en/release-0.5/manual/faq/#how-does-null-or-nothingness-work-in-julia do not work.

A MWE (must be started from REPL, therefore 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 little ugly.

+5
source share
1 answer

Void is a singleton type with one instance. This instance of Void() also called nothing . Keep in mind that nothing === Void()

You can handle it just like any other value.

Returned by a bunch of functions like println .

You can check if anything returns nothing - that is, an instance of type Void .

By

 julia> println()===nothing true 

For type stability, a method should not return nothing part of the time and something part of the time. in this case, instead, it should return a Nullable , as a rule.

+7
source

Source: https://habr.com/ru/post/1257754/


All Articles