Add a match! syntactic sugar to computation expressions
When writing computation expressions, especially asyncs that call my own functions returning Options, I find myself quite often binding it (via let!), then proceeding to use that variable only one time in a pattern match immediately following. It'd be really nice to eliminate the unnecessary let! and have a sort of match! that means the same thing as a let! followed by a vanilla match.
For example, this:
(val myAsyncTryGetResult: 'a -> Async<'a option>)
async {
let! x = myAsyncTryGetResult foo
match x with
| Some(x) -> printfn "got some: %A" x
| None -> printfn "got none" }
could instead be written as:
(val myAsyncTryGetResult: 'a -> Async<'a option>)
async {
match! myAsyncTryGetResult foo with
| Some(x) -> printfn "got some: %A" x
| None -> printfn "got none" }
To implement this, the compiler could by default treat it the same as let! followed by match, or spit out a call to a new builder method for this construct if the builder defines it, perhaps called BindPattern. It would be identical in signature to builder.Bind.
E.g. compiler could emit
asyncBuilder.Bind(myAsyncTryGetResult foo, (fun x -> match x with ... ))
or
asyncBuilder.BindPattern(myAsyncTryGetResult foo, (fun x -> match x with ... ))
Pros and Cons
The advantages of making this adjustment to F# are
- Code that encounters this type of annoyance would be a little cleaner
- Non-invasive and simple
The disadvantages of making this adjustment to F# are
- Nonzero effort required :P
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
Add a
match!syntactic sugar to computation expressionsWhen writing computation expressions, especially asyncs that call my own functions returning
Options, I find myself quite often binding it (vialet!), then proceeding to use that variable only one time in a pattern match immediately following. It'd be really nice to eliminate the unnecessarylet!and have a sort ofmatch!that means the same thing as alet!followed by a vanillamatch.For example, this:
could instead be written as:
To implement this, the compiler could by default treat it the same as
let!followed bymatch,or spit out a call to a new builder method for this construct if the builder defines it, perhaps calledBindPattern. It would be identical in signature tobuilder.Bind.E.g. compiler could emit
or
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantages of making this adjustment to F# are
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply: