The design suggestion match! syntactic sugar has been marked "approved in principle".
This RFC covers the detailed proposal for this suggestion.
- Approved in principle
- Suggestion
- Details: under discussion
- Implementation: Complete
There should be a syntactic sugar, called match!
, that is equivalent to let!
followed by match
.
An oft-repeated idiom in monadic code (especially async
workflows) entails binding a monadic expression, then immediately pattern matching over the result. This happens over and over. For example:
async {
let! x = myAsyncFunction ()
match x with
| Case1 -> ...
| Case2 -> ...
}
match!
is a simple addition to the F# parser, and should be treated as an equivalent let!
and match
. Meaning, this code:
async {
match! myAsyncFunction() with
| Some x -> printfn "%A" x
| None -> printfn "Function returned None!"
}
Would be compiled as if it were written as follows:
async {
let! x = myAsyncFunction ()
match x with
| Some x -> printfn "%A" x
| None -> printfn "Function returned None!"
}
Additional complexity in the language and compiler.
The alternative is to refrain from adding this, where programmers will continue combining let!
and match
.
This is a completely backwards compatible change, and will only require changes to the F# compiler, not the library. No existing code will differ in functionality. Previous versions of the F# compiler that do not implement this syntax emit a compiler error when encountering an instance of it. Additionally, since this is syntactic sugar for existing constructs, compiled binaries using match!
will be compatible with previous F# compilers and tools.
None.