Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/FSharpPlus/Control/Monad.fs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ type TryWith =
static member TryWith (computation: unit -> 'R -> _ , catchHandler: exn -> 'R -> _ , _: Default2, _) = (fun s -> try (computation ()) s with e -> catchHandler e s) : 'R ->_
static member TryWith (computation: unit -> Async<_> , catchHandler: exn -> Async<_> , _: TryWith , _) = async.TryWith ((computation ()), catchHandler)
#if !FABLE_COMPILER
static member TryWith (computation: unit -> Task<_> , catchHandler: exn -> Task<_> , _: TryWith, True) = Task.tryWith computation catchHandler
static member TryWith (computation: unit -> Task<_> , catchHandler: exn -> Task<_> , _: TryWith, True) = Task.tryWith catchHandler computation
static member TryWith (computation: unit -> ValueTask<_> , catchHandler: exn -> ValueTask<_> , _: TryWith, True) = ValueTask.tryWith catchHandler computation
#endif
static member TryWith (computation: unit -> Lazy<_> , catchHandler: exn -> Lazy<_> , _: TryWith , _) = lazy (try (computation ()).Force () with e -> (catchHandler e).Force ()) : Lazy<_>
Expand Down Expand Up @@ -246,7 +246,7 @@ type TryFinally =
static member TryFinally ((computation: unit -> Id<_> , compensation: unit -> unit), _: TryFinally, _, _) = try computation () finally compensation ()
static member TryFinally ((computation: unit -> Async<_>, compensation: unit -> unit), _: TryFinally, _, _) = async.TryFinally (computation (), compensation) : Async<_>
#if !FABLE_COMPILER
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinally, _, True) = Task.tryFinally computation compensation : Task<_>
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinally, _, True) = Task.tryFinally compensation computation : Task<_>
static member TryFinally ((computation: unit -> ValueTask<_>, compensation: unit -> unit), _: TryFinally, _, True) = ValueTask.tryFinally compensation computation : ValueTask<_>
#endif
static member TryFinally ((computation: unit -> Lazy<_> , compensation: unit -> unit), _: TryFinally, _, _) = lazy (try (computation ()).Force () finally compensation ()) : Lazy<_>
Expand Down
26 changes: 24 additions & 2 deletions src/FSharpPlus/Extensions/Task.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharpPlus

#nowarn "44" // Suppress obsolete warning for tryWith and tryFinally
#if !FABLE_COMPILER

/// Additional operations on Task<'T>
Expand Down Expand Up @@ -382,7 +383,7 @@ module Task =
task.ContinueWith k |> ignore
tcs.Task

/// Used to de-sugar try .. with .. blocks in Computation Expressions.
[<ObsoleteAttribute("Swap parameters")>]
let rec tryWith (body: unit -> Task<'T>) (compensation: exn -> Task<'T>) : Task<'T> =
let unwrapException (agg: AggregateException) =
if agg.InnerExceptions.Count = 1 then agg.InnerExceptions.[0]
Expand All @@ -398,7 +399,7 @@ module Task =
| :? AggregateException as exn -> compensation (unwrapException exn)
| exn -> compensation exn

/// Used to de-sugar try .. finally .. blocks in Computation Expressions.
[<ObsoleteAttribute("Swap parameters")>]
let tryFinally (body: unit -> Task<'T>) (compensation : unit -> unit) : Task<'T> =
let mutable ran = false
let compensation () =
Expand Down Expand Up @@ -455,4 +456,25 @@ module Task =
let tcs = TaskCompletionSource<'T> ()
tcs.SetException e
tcs.Task


/// Workaround to fix signatures without breaking binary compatibility.
[<AutoOpen>]
module Task_v2 =
open System.Threading.Tasks
module Task =

/// <summary>Runs a if the body throws an exception, if the returned task faults or if the returned task is canceled.</summary>
/// <param name="compensation">The compensation function to run on exception.</param>
/// <param name="body">The body function to run.</param>
/// <returns>The resulting task.</returns>
/// <remarks>This function is used to de-sugar try .. with .. blocks in Computation Expressions.</remarks>
let inline tryWith ([<InlineIfLambda>] compensation: exn -> Task<'T>) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryWith body compensation

/// <summary>Runs a compensation function after the body completes, regardless of whether the body completed successfully, faulted, or was canceled.</summary>
/// <param name="compensation">The compensation function to run after the body completes.</param>
/// <param name="body">The body function to run.</param>
/// <returns>The resulting task.</returns>
/// <remarks>This function is used to de-sugar try .. finally .. blocks in Computation Expressions.</remarks>
let inline tryFinally ([<InlineIfLambda>] compensation: unit -> unit) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryFinally body compensation
#endif
Loading