Skip to content

Commit dcc2707

Browse files
committed
Net45 fixes
1 parent f8470a4 commit dcc2707

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

src/FSharpPlus/Control/Monad.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ type TryWith =
232232
static member TryWith (computation: unit -> Async<_> , catchHandler: exn -> Async<_> , _: TryWith , _) = async.TryWith ((computation ()), catchHandler)
233233
#if !FABLE_COMPILER
234234
static member TryWith (computation: unit -> Task<_> , catchHandler: exn -> Task<_> , _: TryWith, True) = Task.tryWith catchHandler computation
235+
#endif
236+
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
235237
static member TryWith (computation: unit -> ValueTask<_> , catchHandler: exn -> ValueTask<_> , _: TryWith, True) = ValueTask.tryWith catchHandler computation
236238
#endif
237239
static member TryWith (computation: unit -> Lazy<_> , catchHandler: exn -> Lazy<_> , _: TryWith , _) = lazy (try (computation ()).Force () with e -> (catchHandler e).Force ()) : Lazy<_>
@@ -272,6 +274,8 @@ type TryFinally =
272274
static member TryFinally ((computation: unit -> Async<_>, compensation: unit -> unit), _: TryFinally, _, _) = async.TryFinally (computation (), compensation) : Async<_>
273275
#if !FABLE_COMPILER
274276
static member TryFinally ((computation: unit -> Task<_> , compensation: unit -> unit), _: TryFinally, _, True) = Task.tryFinally compensation computation : Task<_>
277+
#endif
278+
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
275279
static member TryFinally ((computation: unit -> ValueTask<_>, compensation: unit -> unit), _: TryFinally, _, True) = ValueTask.tryFinally compensation computation : ValueTask<_>
276280
#endif
277281
static member TryFinally ((computation: unit -> Lazy<_> , compensation: unit -> unit), _: TryFinally, _, _) = lazy (try (computation ()).Force () finally compensation ()) : Lazy<_>
@@ -310,6 +314,8 @@ type Using =
310314
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Async<'U>, _: Using ) = async.Using (resource, body)
311315
#if !FABLE_COMPILER
312316
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> Task<'U> , _: Using) = Task.using resource body
317+
#endif
318+
#if !NET45 && !NETSTANDARD2_0 && !FABLE_COMPILER
313319
static member Using (resource: 'T when 'T :> IDisposable, body: 'T -> ValueTask<'U>, _: Using) = ValueTask.using resource body
314320
#endif
315321
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>

src/FSharpPlus/Extensions/Task.fs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace FSharpPlus
33
#nowarn "44" // Suppress obsolete warning for tryWith and tryFinally
44
#if !FABLE_COMPILER
55

6+
67
/// Additional operations on Task<'T>
78
[<RequireQualifiedAccess>]
89
module Task =
@@ -11,12 +12,12 @@ module Task =
1112
open System.Threading
1213
open System.Threading.Tasks
1314
open FSharpPlus.Internals.Errors
14-
15+
1516
/// Active pattern to match the state of a completed Task
1617
let inline private (|Succeeded|Canceled|Faulted|) (t: Task<'a>) =
17-
if t.IsCompletedSuccessfully then Succeeded t.Result
18-
elif t.IsFaulted then Faulted (Unchecked.nonNull (t.Exception))
18+
if t.IsFaulted then Faulted (Unchecked.nonNull (t.Exception))
1919
elif t.IsCanceled then Canceled
20+
elif t.IsCompleted then Succeeded t.Result
2021
else invalidOp "Internal error: The task is not yet completed."
2122

2223
/// <summary>Creates a task workflow from 'source' another, mapping its result with 'f'.</summary>
@@ -477,8 +478,16 @@ module Task =
477478
/// <returns>The task if it is not faulted, else the result of evaluating <paramref name="fallbackThunk"/>.</returns>
478479
/// <remarks><paramref name="fallbackThunk"/> is not evaluated unless <paramref name="source"/> is faulted.</remarks>
479480
///
481+
#if !NET45
480482
let inline orElseWith ([<InlineIfLambda>]fallbackThunk: exn -> Task<'T>) (source: Task<'T>) : Task<'T> =
483+
#else
484+
let inline orElseWith (fallbackThunk: exn -> Task<'T>) (source: Task<'T>) : Task<'T> =
485+
#endif
486+
#if !NET45
481487
let source = nullArgCheck (nameof source) source
488+
#else
489+
raiseIfNull "source" source
490+
#endif
482491
tryWith (fun () -> source) fallbackThunk
483492

484493
/// <summary>Returns <paramref name="source"/> if it is not faulted, otherwise e<paramref name="fallbackTask"/>.</summary>
@@ -488,8 +497,13 @@ module Task =
488497
///
489498
/// <returns>The option if the option is Some, else the alternate option.</returns>
490499
let orElse (fallbackTask: Task<'T>) (source: Task<'T>) : Task<'T> =
500+
#if !NET45
491501
let fallbackTask = nullArgCheck (nameof fallbackTask) fallbackTask
492502
let source = nullArgCheck (nameof source) source
503+
#else
504+
raiseIfNull "fallbackTask" fallbackTask
505+
raiseIfNull "source" source
506+
#endif
493507
orElseWith (fun _ -> fallbackTask) source
494508

495509
/// Creates a Task from a value
@@ -513,12 +527,20 @@ module Task_v2 =
513527
/// <param name="body">The body function to run.</param>
514528
/// <returns>The resulting task.</returns>
515529
/// <remarks>This function is used to de-sugar try .. with .. blocks in Computation Expressions.</remarks>
530+
#if !NET45
516531
let inline tryWith ([<InlineIfLambda>] compensation: exn -> Task<'T>) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryWith body compensation
532+
#else
533+
let inline tryWith (compensation: exn -> Task<'T>) (body: unit -> Task<'T>) = Task.tryWith body compensation
534+
#endif
517535

518536
/// <summary>Runs a compensation function after the body completes, regardless of whether the body completed successfully, faulted, or was canceled.</summary>
519537
/// <param name="compensation">The compensation function to run after the body completes.</param>
520538
/// <param name="body">The body function to run.</param>
521539
/// <returns>The resulting task.</returns>
522540
/// <remarks>This function is used to de-sugar try .. finally .. blocks in Computation Expressions.</remarks>
541+
#if !NET45
523542
let inline tryFinally ([<InlineIfLambda>] compensation: unit -> unit) ([<InlineIfLambda>] body: unit -> Task<'T>) = Task.tryFinally body compensation
543+
#else
544+
let inline tryFinally (compensation: unit -> unit) (body: unit -> Task<'T>) = Task.tryFinally body compensation
545+
#endif
524546
#endif

0 commit comments

Comments
 (0)