Skip to content

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

@Happypig375

Description

@Happypig375

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.

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,

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:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • 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:

  • This is not a breaking change to the F# language design
  • 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 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions