Skip to content

Latest commit

 

History

History
73 lines (52 loc) · 2.32 KB

FS-1047-match-bang.md

File metadata and controls

73 lines (52 loc) · 2.32 KB

F# RFC FS-1047 - (match! syntactic sugar in computational expressions)

The design suggestion match! syntactic sugar has been marked "approved in principle".

This RFC covers the detailed proposal for this suggestion.

Summary

There should be a syntactic sugar, called match!, that is equivalent to let! followed by match.

Motivation

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 -> ...
}

Detailed design

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!"
}

Drawbacks

Additional complexity in the language and compiler.

Alternatives

The alternative is to refrain from adding this, where programmers will continue combining let! and match.

Compatibility

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.

Unresolved questions

None.