Skip to content

Commit b86b451

Browse files
realvictorprmcartermp
authored andcommitted
Information about unwrapping single case unions (dotnet#2912)
* Unwrapping single case unions I think this is important to know because this small details makes unions even more attractive to use. Form and details of this can be discussed. cc @cartermp * Apply suggested changes. Signed-off-by: realvictorprm <mueller.vpr@gmail.com> * Apply last changes * All your typos belong to me * Fixing German habits * Missed that typo!
1 parent 195b220 commit b86b451

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Diff for: docs/fsharp/language-reference/discriminated-unions.md

+18
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ let getShapeHeight shape =
8181

8282
Normally, the case identifiers can be used without qualifying them with the name of the union. If you want the name to always be qualified with the name of the union, you can apply the [RequireQualifiedAccess](https://msdn.microsoft.com/library/8b9b6ade-0471-4413-ac5d-638cd0de5f15) attribute to the union type definition.
8383

84+
### Unwrapping Discriminated Unions
85+
86+
In F# Discriminated Unions are often used in domain-modeling for wrapping a single type. It's easy to extract the underlying value via pattern matching as well. You don't need to use a match expression for a single case:
87+
```fsharp
88+
let ([UnionCaseName] [values]) = [UnionValue]
89+
```
90+
91+
The following example demonstrates this:
92+
93+
```fsharp
94+
type ShaderProgram = | ShaderProgram of id:int
95+
96+
let someMethodUsingShaderProgram shaderProgram =
97+
let (ShaderProgram id) = shaderProgram
98+
// Use the unwrapped value
99+
..
100+
```
101+
84102
## Struct Discriminated Unions
85103

86104
Starting with F# 4.1, you can also represent Discriminated Unions as structs. This is done with the `[<Struct>]` attribute.

0 commit comments

Comments
 (0)