diff --git a/src/FSharpPlus/Control/Monad.fs b/src/FSharpPlus/Control/Monad.fs index a83aa9070..9691ddfbe 100644 --- a/src/FSharpPlus/Control/Monad.fs +++ b/src/FSharpPlus/Control/Monad.fs @@ -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<_> @@ -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<_> diff --git a/src/FSharpPlus/Extensions/Task.fs b/src/FSharpPlus/Extensions/Task.fs index e70b516d8..5bb72053f 100644 --- a/src/FSharpPlus/Extensions/Task.fs +++ b/src/FSharpPlus/Extensions/Task.fs @@ -1,5 +1,6 @@ namespace FSharpPlus +#nowarn "44" // Suppress obsolete warning for tryWith and tryFinally #if !FABLE_COMPILER /// Additional operations on Task<'T> @@ -382,7 +383,7 @@ module Task = task.ContinueWith k |> ignore tcs.Task - /// Used to de-sugar try .. with .. blocks in Computation Expressions. + [] 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] @@ -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. + [] let tryFinally (body: unit -> Task<'T>) (compensation : unit -> unit) : Task<'T> = let mutable ran = false let compensation () = @@ -455,4 +456,25 @@ module Task = let tcs = TaskCompletionSource<'T> () tcs.SetException e tcs.Task + + +/// Workaround to fix signatures without breaking binary compatibility. +[] +module Task_v2 = + open System.Threading.Tasks + module Task = + + /// Runs a if the body throws an exception, if the returned task faults or if the returned task is canceled. + /// The compensation function to run on exception. + /// The body function to run. + /// The resulting task. + /// This function is used to de-sugar try .. with .. blocks in Computation Expressions. + let inline tryWith ([] compensation: exn -> Task<'T>) ([] body: unit -> Task<'T>) = Task.tryWith body compensation + + /// Runs a compensation function after the body completes, regardless of whether the body completed successfully, faulted, or was canceled. + /// The compensation function to run after the body completes. + /// The body function to run. + /// The resulting task. + /// This function is used to de-sugar try .. finally .. blocks in Computation Expressions. + let inline tryFinally ([] compensation: unit -> unit) ([] body: unit -> Task<'T>) = Task.tryFinally body compensation #endif