Search code examples
julianifti

Static member type or nothing in Julia


Just trying to get my head around Julia and came across the need where a type member should be of a particular type or it can also be nothing. So, I tried the following:

using NIfTI  # Julia package for reading NIfTI medical images

type RR
    source::Union(NIfTI.NIVolume, nothing)
end

However, when I try to initialize this object, I get:

ERROR: LoadError: LoadError: MethodError: no method matching Union(::Type{NIfTI.NIVolume}, ::Void)

The reason I want to do this is that there is no good default way to initialize the NIVolume object and it seems a good idea to leave it uninitialized till needed.


Solution

  • Julia's type system can also express the concept that an expression cannot produce any value – e.g. if it throws an error or is part of a basic block that cannot execute (dead code). The type of an expression that can never produce a value is the empty union type, Union{}: a union of zero types, of which no values are instances. This is distinct from the type of nothing – since nothing is a normal (but uninteresting) value, so it cannot be an instance of Union{}.

    http://docs.julialang.org/en/release-0.4/manual/faq/#nothingness-and-missing-values