fsharp / fsharp/fslang-suggestions

finally!

Open
#651 14 comments 20 reactions 0 assignees View on GitHub
approved-in-principle area: computation-expressions
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we add `try .. finally! ...` to allow `finally` actions to be monadic.

The current computation-expression desugaring of `try .. finally ... ` allows only a `unit -> unit` action in the compensation branch.. However there are cases where an asynchronous or other monadic imperative action makes sense in exception compensation, e.g. using AsyncSeq:

```fsharp
asyncSeq {
try
do! Async.Sleep 2000;
yield UpdateItemsReceived [| { Text = "AsyncLoad1"; Description = "AsyncDescription1" } |]
do! Async.Sleep 2000;
yield UpdateItemsReceived [| { Text = "AsyncLoad2"; Description = "AsyncDescription2" } |]
do! Async.Sleep 2000;
finally!
yield UpdateItemsComplete
}

```

The current way of doing this in F# is some mess like this:

asyncSeq {
do! Async.Sleep 2000;
yield UpdateItemsReceived [| { Text = "AsyncLoad1"; Description = "AsyncDescription1" } |]
do! Async.Sleep 2000;
yield UpdateItemsReceived [| { Text = "AsyncLoad2"; Description = "AsyncDescription2" } |]
do! Async.Sleep 2000;
}
|> (fun a -> AsyncSeq.tryFinallyMonadic(a, fun () -> ...))

The same effect can also currently be achieved with a custom operation, something like this, but just supporting `finally!` looks more sensible:

asyncSeq {
try
do! Async.Sleep 2000;
yield UpdateItemsReceived [| { Text = "AsyncLoad1"; Description = "AsyncDescription1" } |]
do! Async.Sleep 2000;
yield UpdateItemsReceived [| { Text = "AsyncLoad2"; Description = "AsyncDescription2" } |]
do! Async.Sleep 2000;
yieldFinally UpdateItemsComplete
}

## Pros and Cons

The advantages of making this adjustment to F# are orthogonality/simplicity.

The disadvantages of making this adjustment to F# are cost.

## Extra information

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

## Related Items

* https://github.com/fsharp/fslang-suggestions/issues/579

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the proposed `finally!` syntax, the computation-expression desugaring described here, and related issue #579. No implementation files, tests, or compiler entry points are named, so identify the relevant language-design and compiler areas before proceeding. Done would require an accepted design and corresponding implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.