From 09f871ef7e74ea915871df1ad156c2f355a90e26 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Fri, 24 Jan 2025 10:52:52 -0800 Subject: [PATCH 1/2] Fixed bug 40039. --- docs/fsharp/language-reference/options.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/fsharp/language-reference/options.md b/docs/fsharp/language-reference/options.md index b7184b5223c80..39ea727a30392 100644 --- a/docs/fsharp/language-reference/options.md +++ b/docs/fsharp/language-reference/options.md @@ -61,6 +61,21 @@ The option module also includes functions that correspond to the functions that 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). +### Converting Options with Default Values + +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: + +```fsharp +let optionString = Some("F#") +let defaultString = optionString |> Option.defaultValue "" +// defaultString is "F#" + +let optionInt = None +let defaultInt = optionInt |> Option.defaultValue 0 +// defaultInt is 0 + +The `Option.defaultValue` function allows you to handle both `Some` and `None` cases seamlessly without pattern matching. + ## See also - [F# Language Reference](index.md) From d3b859931efa0c2d3e32c8d75b0913a27a6691df Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 27 Jan 2025 09:28:00 -0500 Subject: [PATCH 2/2] Add example for `Option.defaultValue` function --- docs/fsharp/language-reference/options.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/fsharp/language-reference/options.md b/docs/fsharp/language-reference/options.md index 39ea727a30392..6136ad8046f22 100644 --- a/docs/fsharp/language-reference/options.md +++ b/docs/fsharp/language-reference/options.md @@ -73,6 +73,7 @@ let defaultString = optionString |> Option.defaultValue "" let optionInt = None let defaultInt = optionInt |> Option.defaultValue 0 // defaultInt is 0 +``` The `Option.defaultValue` function allows you to handle both `Some` and `None` cases seamlessly without pattern matching.