-
Notifications
You must be signed in to change notification settings - Fork 4
Option
#Option
Represents optional values.
Instances of Option are either an instance of Some or None.
Some holds a non-null value whilst None holds no value.
The purpose is of Optional/Some/None pattern is to replace the ugly null checking that one is forced to in Java.
An example is the Map interface in Java. Performing a get(...) on the Map will yield either the value of the key or null in case no such key exists.
In Scala you'll get an Option which may contain a value or None in case there was no such key.
Represents an Option holding a non-null value.
Null values are not allowed as they're by default a non-value and therefore are mapped to None.
Creating Options of type Some is straightforward.
Either use the implementation directly:
Option<String> option = new Some("Peter is Great!");
or use the factory/apply method from Option:
Option<String> option = Option.apply("Peter is Great!");
Represents an empty Option.
Empty in the sense of not holding any value. In Java you would normally represent this by null.
But null is ugly and cannot be used in programming without constant null checking.
None allows for programmatic usage performing no actions.
Creating Options of type None is straightforward.
Either use the implementation directly:
Option<String> option = new None<>();
or use the empty method from the Option:
Option<String> option = Option.empty();
or can use the apply method and supply null.
Option<String> option = Option.apply(null);
Note the null value provided to apply will yield an Option of type None.
Any of the factory methods (apply/empty) methods is preferred as it will return the singleton None since it anyways cannot represent a state, therefore reducing creation of unnecessary object instances.
##Examples
The most obvious use case for Options are as return type from methods instead of possible null values.
E.g. the following operations returns a UserData object if a user with the provided name exists, None otherwise.
Option<UserData> getUser(String userName)
###Replacing ugly if-not-null checks
Consider the following code snippet. The Listener in this case is optional.
The classical way would have been to have code that did something like this
private final SomeListener listener;
public SomeClass(SomeListener listener) {
this.listener = listener;
}
public void notifySomething(String message) {
if(listener != null)
listener.notify(message);
}
Now consider using Option that allows for neater programming constructs.
private final Option<SomeListener> listener;
public SomeClass(Option<SomeListener> listener) {
this.listener = listener;
}
public void notifySomething(final String message) {
listener.forEach(l - l.notify(message))
}
One can in a single statement replace the if-null-then-default code construct by getOrElse
Option<String> option = ...;
String value = option.getOrElse(() -> "Some default value");
- Introduction
- JavaDoc (Latest)
- Containers
- Option/Some/None
- Try/Success/Failure
- Either/Left/Right
- Asynchronous Execution
- Future/Promise
- Companion Classes
- The Unit Type
- Testing/Asserts
- License
- Chat room for the project:
- Continuous Integration: