Skip to content

Commit e3ae3bd

Browse files
shethaaditAdit ShethBillWagner
authored
Enhance F# Options Documentation: Add Conversion Examples to "Converting to Other Types" Section (#44512)
* Fixed bug 40039. * Add example for `Option.defaultValue` function --------- Co-authored-by: Adit Sheth <[email protected]> Co-authored-by: Bill Wagner <[email protected]>
1 parent 6b378b8 commit e3ae3bd

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

docs/fsharp/language-reference/options.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ The option module also includes functions that correspond to the functions that
6161

6262
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`](https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-optionmodule.html#toArray). To convert an option to a list, use [`Option.toList`](https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-optionmodule.html#toList).
6363

64+
### Converting Options with Default Values
65+
66+
In addition to converting to lists and arrays, options can be converted to other types by providing default values using the `Option.defaultValue` function. This is particularly useful when you want to ensure that the value is not `None`. For example:
67+
68+
```fsharp
69+
let optionString = Some("F#")
70+
let defaultString = optionString |> Option.defaultValue ""
71+
// defaultString is "F#"
72+
73+
let optionInt = None
74+
let defaultInt = optionInt |> Option.defaultValue 0
75+
// defaultInt is 0
76+
```
77+
78+
The `Option.defaultValue` function allows you to handle both `Some` and `None` cases seamlessly without pattern matching.
79+
6480
## See also
6581

6682
- [F# Language Reference](index.md)

0 commit comments

Comments
 (0)