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
15 changes: 12 additions & 3 deletions src/FSharpPlus/Control/Monad.fs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ 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
#endif
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
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 @@ -270,7 +273,10 @@ 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<_>
#endif
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
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 Expand Up @@ -307,7 +313,10 @@ type Using =
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> 'R -> 'U , _: Using ) = (fun s -> try body resource s finally if not (isNull (box resource)) then resource.Dispose ()) : 'R->'U
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Async<'U>, _: Using ) = async.Using (resource, body)
#if !FABLE_COMPILER
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Task<'U>, _: Using ) = Task.using resource body
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Task<'U> , _: Using) = Task.using resource body
#endif
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> ValueTask<'U>, _: Using) = ValueTask.using resource body
#endif
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Lazy<'U> , _: Using ) = lazy (try (body resource).Force () finally if not (isNull (box resource)) then resource.Dispose ()) : Lazy<'U>

Expand Down
119 changes: 94 additions & 25 deletions src/FSharpPlus/Extensions/Task.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace FSharpPlus

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


/// Additional operations on Task<'T>
[<RequireQualifiedAccess>]
module Task =
Expand All @@ -10,11 +12,13 @@ module Task =
open System.Threading
open System.Threading.Tasks
open FSharpPlus.Internals.Errors

let private (|Canceled|Faulted|Completed|) (t: Task<'a>) =
if t.IsCanceled then Canceled
else if t.IsFaulted then Faulted (Unchecked.nonNull t.Exception)
else Completed t.Result

/// Active pattern to match the state of a completed Task
let inline private (|Succeeded|Canceled|Faulted|) (t: Task<'a>) =
if t.IsFaulted then Faulted (Unchecked.nonNull (t.Exception))
elif t.IsCanceled then Canceled
elif t.IsCompleted then Succeeded t.Result
else invalidOp "Internal error: The task is not yet completed."

/// <summary>Creates a task workflow from 'source' another, mapping its result with 'f'.</summary>
let map (f: 'T -> 'U) (source: Task<'T>) : Task<'U> =
Expand Down Expand Up @@ -42,7 +46,7 @@ module Task =
let k = function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
try tcs.SetResult (f r)
with e -> tcs.SetException e
source.ContinueWith k |> ignore
Expand Down Expand Up @@ -79,15 +83,15 @@ module Task =
let k = function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
try tcs.SetResult (f x.Result r)
with e -> tcs.SetException e
y.ContinueWith k |> ignore
| _, TaskStatus.RanToCompletion ->
let k = function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
try tcs.SetResult (f r y.Result)
with e -> tcs.SetException e
x.ContinueWith k |> ignore
Expand All @@ -96,12 +100,12 @@ module Task =
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
y.ContinueWith (
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r' ->
| Succeeded r' ->
try tcs.SetResult (f r r')
with e -> tcs.SetException e
) |> ignore) |> ignore
Expand Down Expand Up @@ -144,17 +148,17 @@ module Task =
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
y.ContinueWith (
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r' ->
| Succeeded r' ->
z.ContinueWith (
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r'' ->
| Succeeded r'' ->
try tcs.SetResult (f r r' r'')
with e -> tcs.SetException e
) |> ignore) |> ignore) |> ignore
Expand Down Expand Up @@ -203,7 +207,7 @@ module Task =
match t with
| Canceled -> cancelled <- true
| Faulted e -> failures[i] <- e.InnerExceptions
| Completed r -> v.Value <- r
| Succeeded r -> v.Value <- r
trySet ()

if task1.IsCompleted && task2.IsCompleted then
Expand Down Expand Up @@ -261,7 +265,7 @@ module Task =
match t with
| Canceled -> cancelled <- true
| Faulted e -> failures[i] <- e.InnerExceptions
| Completed r -> v.Value <- r
| Succeeded r -> v.Value <- r
trySet ()

if task1.IsCompleted && task2.IsCompleted && task3.IsCompleted then
Expand Down Expand Up @@ -304,15 +308,15 @@ module Task =
let k = function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
try tcs.SetResult (f.Result r)
with e -> tcs.SetException e
x.ContinueWith k |> ignore
| _, TaskStatus.RanToCompletion ->
let k = function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
try tcs.SetResult (r x.Result)
with e -> tcs.SetException e
f.ContinueWith k |> ignore
Expand All @@ -321,12 +325,12 @@ module Task =
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
x.ContinueWith (
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r' ->
| Succeeded r' ->
try tcs.SetResult (r r')
with e -> tcs.SetException e
) |> ignore) |> ignore
Expand Down Expand Up @@ -355,24 +359,24 @@ module Task =
let k = function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r -> tcs.SetResult (x.Result, r)
| Succeeded r -> tcs.SetResult (x.Result, r)
y.ContinueWith k |> ignore
| _, TaskStatus.RanToCompletion ->
let k = function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r -> tcs.SetResult (r, y.Result)
| Succeeded r -> tcs.SetResult (r, y.Result)
x.ContinueWith k |> ignore
| _, _ ->
x.ContinueWith (
function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r ->
| Succeeded r ->
y.ContinueWith (function
| Canceled -> tcs.SetCanceled ()
| Faulted e -> tcs.SetException e.InnerExceptions
| Completed r' -> tcs.SetResult (r, r')) |> ignore) |> ignore
| Succeeded r' -> tcs.SetResult (r, r')) |> ignore) |> ignore
tcs.Task

/// <summary>Creates a task workflow from two workflows 'task1' and 'task2', tupling its results.</summary>
Expand Down Expand Up @@ -424,7 +428,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 @@ -440,7 +444,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 All @@ -466,6 +470,42 @@ module Task =
(fun () -> body disp)
(fun () -> if not (isNull (box disp)) then disp.Dispose ())

/// <summary>Returns <paramref name="source"/> if it is not faulted, otherwise evaluates <paramref name="fallbackThunk"/> and returns the result.</summary>
///
/// <param name="fallbackThunk">A thunk that provides an alternate task computation when evaluated.</param>
/// <param name="source">The input task.</param>
///
/// <returns>The task if it is not faulted, else the result of evaluating <paramref name="fallbackThunk"/>.</returns>
/// <remarks><paramref name="fallbackThunk"/> is not evaluated unless <paramref name="source"/> is faulted.</remarks>
///
#if !NET45
let inline orElseWith ([<InlineIfLambda>]fallbackThunk: exn -> Task<'T>) (source: Task<'T>) : Task<'T> =
#else
let inline orElseWith (fallbackThunk: exn -> Task<'T>) (source: Task<'T>) : Task<'T> =
#endif
#if !NET45
let source = nullArgCheck (nameof source) source
#else
raiseIfNull "source" source
#endif
tryWith (fun () -> source) fallbackThunk

/// <summary>Returns <paramref name="source"/> if it is not faulted, otherwise e<paramref name="fallbackTask"/>.</summary>
///
/// <param name="fallbackTask">The alternative Task to use if <paramref name="source"/> is faulted.</param>
/// <param name="source">The input task.</param>
///
/// <returns>The option if the option is Some, else the alternate option.</returns>
let orElse (fallbackTask: Task<'T>) (source: Task<'T>) : Task<'T> =
#if !NET45
let fallbackTask = nullArgCheck (nameof fallbackTask) fallbackTask
let source = nullArgCheck (nameof source) source
#else
raiseIfNull "fallbackTask" fallbackTask
raiseIfNull "source" source
#endif
orElseWith (fun _ -> fallbackTask) source

/// Creates a Task from a value
let result (value: 'T) = Task.FromResult value

Expand All @@ -474,4 +514,33 @@ 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>
#if !NET45
let inline tryWith ([<InlineIfLambda>] compensation: exn -> Task<'T>) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryWith body compensation
#else
let inline tryWith (compensation: exn -> Task<'T>) (body: unit -> Task<'T>) = Task.tryWith body compensation
#endif

/// <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>
#if !NET45
let inline tryFinally ([<InlineIfLambda>] compensation: unit -> unit) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryFinally body compensation
#else
let inline tryFinally (compensation: unit -> unit) (body: unit -> Task<'T>) = Task.tryFinally body compensation
#endif
#endif
Loading
Loading