Learn how to use F# option types when an actual value might not exist for a named value or variable.
The option type in F# is used when an actual value might not exist for a named value or variable. An option has an underlying type and can hold a value of that type, or it might not have a value.
The following code illustrates a function which generates an option type.
As you can see, if the input
a is greater than 0,
Some(a) is generated. Otherwise,
None is generated.
None is used when an option does not have an actual value. Otherwise, the expression
Some( ... ) gives the option a value. The values
None are useful in pattern matching, as in the following function
exists, which returns
true if the option has a value and
false if it does not.
Options are commonly used when a search does not return a matching result, as shown in the following code.
In the previous code, a list is searched recursively. The function
tryFindMatch takes a predicate function
pred that returns a Boolean value, and a list to search. If an element that satisfies the predicate is found, the recursion ends and the function returns the value as an option in the expression
Some(head). The recursion ends when the empty list is matched. At that point the value
head has not been found, and
None is returned.
Many F# library functions that search a collection for a value that may or may not exist return the
option type. By convention, these functions begin with the
try prefix, for example,
Options can also be useful when a value might not exist, for example if it is possible that an exception will be thrown when you try to construct a value. The following code example illustrates this.
openFile function in the previous example has type
string -> File option because it returns a
File object if the file opens successfully and
None if an exception occurs. Depending on the situation, it may not be an appropriate design choice to catch an exception rather than allowing it to propagate.
Option Properties and Methods
The option type supports the following properties and methods.
|Property or method||Type||Description|
||A static property that enables you to create an option value that has the
||A static member that creates an option that has a value that is not
||Returns the underlying value, or throws a
There is a module, Option, that contains useful functions that perform operations on options. Some functions repeat the functionality of the properties but are useful in contexts where a function is needed. Option.isSome and Option.isNone are both module functions that test whether an option holds a value. Option.get obtains the value, if there is one. If there is no value, it throws
The Option.bind function executes a function on the value, if there is a value. The function must take exactly one argument, and its parameter type must be the option type. The return value of the function is another option type.
The option module also includes functions that correspond to the functions that are available for lists, arrays, sequences, and other collection types. These functions include
Option.count. These functions enable options to be used like a collection of zero or one elements. For more information and examples, see the discussion of collection functions in Lists.
Converting to Other Types
Options can be converted to lists or arrays. When an option is converted into either of these data structures, the resulting data structure has zero or one element. To convert an option to an array, use
Option.toArray. To convert an option to a list, use