fsharp / fsharp/fslang-suggestions

Add "," as separator for pattern matching on multiple named discriminated unions fields and deprecate ";"

Open
#957 5 comments 3 reactions 1 assignee Claimed by @edgarfgp View on GitHub
approved-in-principle area: pattern-matching
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# Make it possible to deconstruct multi-field Discriminated Unions by naming multiple fields

Right above https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/discriminated-unions#unwrapping-discriminated-unions, it is documented that we can match a discriminated unions by a single field name.

```fs
type Shape =
| Rectangle of width : float * length : float
| Circle of radius : float
| Prism of width : float * float * height : float

let getShapeWidth shape =
match shape with
| Rectangle(width = w) -> w
| Circle(radius = r) -> 2. * r
| Prism(width = w) -> w
```

However, as soon as we try to match more than one field,
```fs
let getContainedSpace shape =
match shape with
| Rectangle(w, l) -> w * l
| Circle(r) -> System.Math.PI * r ** 2.
| Prism(w, l, height = h) -> w * l * h // The "height = " causes these errors
// error FS0010: Unexpected symbol '=' in pattern. Expected ')' or other token.
// error FS0583: Unmatched '('
// error FS0010: Unexpected symbol '=' in pattern. Expected ')' or other token.
// error FS0583: Unmatched '('
// error FS0039: The value or constructor 'h' is not defined.
```
This feature is so incomplete!

## Pros and Cons

The advantages of making this adjustment to F# are
1. Extending an existing feature to satisfy more use cases
2. More consistency between constructing and deconstructing a discriminated union. This is already allowed for the construction side.

The disadvantages of making this adjustment to F# are (none)

## Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: (put links to related suggestions here)

## Affidavit (please submit!)

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

## For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.