Skip to content

Commit 3e94405

Browse files
committed
Fix parameters order in tryWith/Finally for Task
1 parent 75bbcee commit 3e94405

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/FSharpPlus/Control/Monad.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ type TryWith =
216216
static member TryWith (computation: unit -> 'R -> _ , catchHandler: exn -> 'R -> _ , _: Default2, _) = (fun s -> try (computation ()) s with e -> catchHandler e s) : 'R ->_
217217
static member TryWith (computation: unit -> Async<_> , catchHandler: exn -> Async<_> , _: TryWith , _) = async.TryWith ((computation ()), catchHandler)
218218
#if !FABLE_COMPILER
219-
static member TryWith (computation: unit -> Task<_> , catchHandler: exn -> Task<_> , _: TryWith, True) = Task.tryWith computation catchHandler
219+
static member TryWith (computation: unit -> Task<_> , catchHandler: exn -> Task<_> , _: TryWith, True) = Task.tryWith catchHandler computation
220220
static member TryWith (computation: unit -> ValueTask<_> , catchHandler: exn -> ValueTask<_> , _: TryWith, True) = ValueTask.tryWith catchHandler computation
221221
#endif
222222
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 =
246246
static member TryFinally ((computation: unit -> Id<_> , compensation: unit -> unit), _: TryFinally, _, _) = try computation () finally compensation ()
247247
static member TryFinally ((computation: unit -> Async<_>, compensation: unit -> unit), _: TryFinally, _, _) = async.TryFinally (computation (), compensation) : Async<_>
248248
#if !FABLE_COMPILER
249-
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinally, _, True) = Task.tryFinally computation compensation : Task<_>
249+
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinally, _, True) = Task.tryFinally compensation computation : Task<_>
250250
static member TryFinally ((computation: unit -> ValueTask<_>, compensation: unit -> unit), _: TryFinally, _, True) = ValueTask.tryFinally compensation computation : ValueTask<_>
251251
#endif
252252
static member TryFinally ((computation: unit -> Lazy<_> , compensation: unit -> unit), _: TryFinally, _, _) = lazy (try (computation ()).Force () finally compensation ()) : Lazy<_>

src/FSharpPlus/Extensions/Task.fs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace FSharpPlus
22

3+
#nowarn "44" // Suppress obsolete warning for tryWith and tryFinally
34
#if !FABLE_COMPILER
45

56
/// Additional operations on Task<'T>
@@ -382,7 +383,7 @@ module Task =
382383
task.ContinueWith k |> ignore
383384
tcs.Task
384385

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

401-
/// Used to de-sugar try .. finally .. blocks in Computation Expressions.
402+
[<ObsoleteAttribute("Swap parameters")>]
402403
let tryFinally (body: unit -> Task<'T>) (compensation : unit -> unit) : Task<'T> =
403404
let mutable ran = false
404405
let compensation () =
@@ -455,4 +456,25 @@ module Task =
455456
let tcs = TaskCompletionSource<'T> ()
456457
tcs.SetException e
457458
tcs.Task
459+
460+
461+
/// Workaround to fix signatures without breaking binary compatibility.
462+
[<AutoOpen>]
463+
module Task_v2 =
464+
open System.Threading.Tasks
465+
module Task =
466+
467+
/// <summary>Runs a if the body throws an exception, if the returned task faults or if the returned task is canceled.</summary>
468+
/// <param name="compensation">The compensation function to run on exception.</param>
469+
/// <param name="body">The body function to run.</param>
470+
/// <returns>The resulting task.</returns>
471+
/// <remarks>This function is used to de-sugar try .. with .. blocks in Computation Expressions.</remarks>
472+
let inline tryWith ([<InlineIfLambda>] compensation: exn -> Task<'T>) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryWith body compensation
473+
474+
/// <summary>Runs a compensation function after the body completes, regardless of whether the body completed successfully, faulted, or was canceled.</summary>
475+
/// <param name="compensation">The compensation function to run after the body completes.</param>
476+
/// <param name="body">The body function to run.</param>
477+
/// <returns>The resulting task.</returns>
478+
/// <remarks>This function is used to de-sugar try .. finally .. blocks in Computation Expressions.</remarks>
479+
let inline tryFinally ([<InlineIfLambda>] compensation: unit -> unit) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryFinally body compensation
458480
#endif

0 commit comments

Comments
 (0)