Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow assigning arbitrary values when pattern matching #1378

Open
6 tasks done
Larocceau opened this issue Aug 14, 2024 · 5 comments
Open
6 tasks done

Allow assigning arbitrary values when pattern matching #1378

Larocceau opened this issue Aug 14, 2024 · 5 comments

Comments

@Larocceau
Copy link

Larocceau commented Aug 14, 2024

I propose we make it possible to assign arbitrary values in match statements, so that OR matching becomes a bit more versatile by allowing you to define default values for valueless cases; eg:

type myType =
    | CaseWithValue of int
    | CaseWithoutValue
    
let myFuction =
    function
    | CaseWithValue v
    | CaseWithoutValue with let v = 1 ->
        v + 1

The existing way of approaching this problem in F# is to handle the cases separately

type myType =
    | CaseWithValue of int
    | CaseWithoutValue
    
let myFuction =
    function
    | CaseWithValue v ->  v + 1
    | CaseWithoutValue -> 2 

Pros and Cons

The advantages of making this adjustment to F# are the*

  • We will be able to make better use of OR matching.

The disadvantages of making this adjustment to F# are

  • This will use up a keyword

Extra information

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

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick these items 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
  • This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
  • 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
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate

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.

@brianrourkeboll
Copy link

One existing way would probably be to use an active pattern and an & or as pattern:

let f x =
    let (|Otherwise|) _ = 1

    match x with
    | A v
    | B & Otherwise v -> v + 1

@Tarmil
Copy link

Tarmil commented Aug 15, 2024

@brianrourkeboll You can even pass the value as argument to this active pattern.

let (|With|) v _ = v

let f x =
    match x with
    | A v
    | B & With 1 v -> v + 1

@edgarfgp
Copy link

I think parameterless total active patterns should only be allowed on functions ? See dotnet/fsharp#17190

@LyndonGingerich
Copy link

LyndonGingerich commented Aug 20, 2024

The disadvantages of making this adjustment to F# are

  • This will use up a keyword

Do you mean let or with? Both are already reserved keywords.

@voronoipotato
Copy link

voronoipotato commented Aug 20, 2024

Merging cases that aren't the same is a dangerous thing to do, just make a function and reuse it. I don't think we should do this and I think adding it would be damaging to the language.

module MyType = 
  let getValueOrDefault defaultValue m =
    match m with
    | CaseWithValue a -> a
    | CaseWithoutValue -> defaultValue 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants