Option

The Option structure provides some useful functions for dealing with optional values. For any value v the expression (SOME v) represents the value being present and NONE for an absent value.

When a function returns an optional value you can use getOpt to reduce it to a definite value. It does this by providing a default if the optional value is omitted. For example to look up a name in a translation table and return the original name if it isn't present write something like getOpt(STRT.find table name, name).

If you are 100% confident that the optional result from a function is present you can use valOf to reduce it to a definite value. It will raise the Option exception if you are wrong.

The isSome function can be useful to detect if an optional value is present. If the base type of the option is an equality type you could write (f x) = NONE. But this won't work for all types for example, at the top level:

- val z = SOME (fn x => x);
val z = SOME fn : ('a -> 'a) option
- z = NONE;
stdIn:25.1-25.9 Error: operator and operand
don't agree [equality type required]
  operator domain: ''Z * ''Z
  operand:         ('Y -> 'Y) option * 'X option
  in expression:
    z = NONE
- Option.isSome z;
val it = true : bool

Here z is an optional function and you can't test two functions to see if they are the same so you can't write z = NONE either. You must either use isSome or use a case expression.

The other functions in the Option structure might be useful in special circumstances. The map function looks to be the most useful.