1
votes

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.

1
there is a Nullable type in julia.Gnimuc
btw, it seems that NIfTI.jl provides a default way to init NIVolume.Gnimuc
these lines are optional or keyword arguments, so you can simply init a nii object via NIVolume() or NIVolume(rand(3,3,3), descrip="my nii obj")(the first optional argument is the raw data; descrip is keyword argument.)Gnimuc
@Gnimuc Right. Got you! Sorry was being very stupid and a bit confused!Luca
feel free to ask if you have any questions ;)Gnimuc

1 Answers

3
votes

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