Allow assigning arbitrary values when pattern matching
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:
- [x] This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
- [x] 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
- [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
- [x] I have searched both open and closed suggestions on this site and believe this is not a duplicate
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.
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
@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
I think parameterless total active patterns should only be allowed on functions ? See https://github.com/dotnet/fsharp/issues/17190
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.
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
I am marking this as probably not - the examples given here show that existing F# syntax can express the desired intent.
https://github.com/fsharp/fslang-suggestions/issues/1378#issuecomment-2299136473 https://github.com/fsharp/fslang-suggestions/issues/1378#issuecomment-2291487458
Allowing arbitrary expressions after with let v = ... would also further complicate pattern match compilation and related optimizations. The size estimate would have to be bigger here.