Clone wiki

bolts / Option

Home * На русском


Option class either stores some value (and it is Some(value) then), or it stores nothing (and it is None). Option is useful, for example, when querying database for one row maximum, and empty result is not an error. In this case result type may be declared Option.

Using null as absence of result value is common Java practice. This is exactly the case when Options should be used.

Option Class Method

... are created to make easy value extraction.

get() returns value from Some, or throws if called on None.

getOrElse(defaultValue) returns value from Some, or returns defaultValue otherwise.

isEmpty, isDefined checks if instance is Some or None.

getOrThrow(String message) works like get(), but throws an exception with specified message in case of None.

Option instances may be received via static factory methods: some(value) or none().

Option in Another Languages

Option type exists in the many modern programming environments (Scala, OCaml, Haskell, C++/Boost).