First of all I'm developing a parser for an XML-based format for 3D graphics called XGL.
But this question can be applied to any situation when you have fields in your class that are optional i.e. the value of this field can be missing.
As I was taking a Scala course on coursera there was an interesting pattern when you create an abstract class with all the methods you need and then create a normal fully functional subclass and an "empty" singleton subclass that always returns false
for isEmpty
method and throws exceptions for the other ones.
So my question is: is it better to just assign null
if the optional field's value is missing or make a hierarchy described above and assign it an empty singleton implementation?
Best Answer
I also attended the same Scala course and I agree with e-i-s that you should use the
Option[T]
type for implementing an optional field. Here are some more ideas.The pattern involving
isEmpty
isEmpty
always returnstrue
is also used to implement sequence-like immutable structures such as lists, streams, and so on.
Compare:
Option[T]
type containing the valuesSome(t)
, for each valuet
inT
, and the additional valueNone
.List[T]
type containing objects built from two subclasses (and their respective constructors)Cons[T](h: T, t: List[T])
andNil
: these represent immutable lists whose elements have typeT
. Note that here the methodNil.isEmpty
returns true.None
indicates the absence of a value, whileNil
indicates an empty list.So an optional value
None
is not the same as an empty listNil
.NOTE
If you need to test if an optional value is defined or not you can use the
isEmpty
method of theOption
type, or pattern matching:and then, to test if the field of an instance
obj
ofMyClass
is defined:or