Option Type Conventions
Option types are an alternative to the use of null pointers, and are generally preferred (especially when storing long-term state). The main advantage is that they document and statically check the possibility that a value is not present.
Options are implemented in the edu.rice.cs.plt.tuple package. The unary tuple type, Wrapper<T>, represents the "some" case; the 0-ary tuple type, Null<T>, represents the "none" case. All relevant operations are defined in these classes' abstract parent, Option<T>.
For further information, consult the Option class javadocs.
Construction
Option.some(val) Option.none() Option.wrap(val)
The some method always produces a Wrapper. wrap, on the other hand, may produce a Wrapper or a Null, depending on whether the argument is null. When val is known to be non-null, the two are equivalent, but, for clarity, some is preferred.
none is implemented as an unchecked cast from a singleton to Option<T> for the provided T, much like Collections.emptyList().
Inference will choose the type of val for T when invoking some, which is usually what you want. If this type is too specific, either use an explicit argument or change the expected type to a wildcard:
Option<Integer> opt = Option.some(23) Option<Number> opt = Option.<Number>some(23) Option<? extends Number> opt = Option.some(23)
When invoking none, T is correctly inferred if immediately assigning or returning the result; otherwise (as in a nested method call), an explicit value for T needs to be provided:
Option<String> opt = Option.none() Option<String> opt = identity(Option.<String>none()) Option<Object> opt = identity(Option.none())
Decomposition
opt.unwrap(noneCase)
if (opt.isSome()) { ... opt.unwrap() ... }
else { ... }
try { ... opt.unwrap() ... }
catch (OptionUnwrapException e) { ... }
opt.apply(new OptionVisitor<Foo, Bar>() {
public Bar forSome(Foo val) { ... }
public Bar forNone() { ... }
}
Wherever possible, the noneCase version of unwrap is preferred over the others. Often there's a default value that should be used; sometimes, null works (but, of course, just unwrapping to a null and then using the result instead of the original Option kind of defeats the purpose):
opt.unwrap(null) instanceof Foo // value exists and is a Foo
The if/else approach is preferred over try/catch, unless the "none" case is an error (and the catch clause would then typically just throw another exception).
Some of us like using visitors to encode typecase expressions, but the additional cost in larger compiled code size (an extra .class file) and (probably) slower execution time makes it hard to justify when using option types. if/else is usually a better approach.
