Skip to content

Commit 75bbcee

Browse files
committed
+ orElse(With) for (Value)Task
1 parent 28ad630 commit 75bbcee

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/FSharpPlus/Extensions/Task.fs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,29 @@ module Task =
424424
(fun () -> body disp)
425425
(fun () -> if not (isNull (box disp)) then disp.Dispose ())
426426

427+
/// <summary>Returns <paramref name="source"/> if it is not faulted, otherwise evaluates <paramref name="fallbackThunk"/> and returns the result.</summary>
428+
///
429+
/// <param name="fallbackThunk">A thunk that provides an alternate task computation when evaluated.</param>
430+
/// <param name="source">The input task.</param>
431+
///
432+
/// <returns>The task if it is not faulted, else the result of evaluating <paramref name="fallbackThunk"/>.</returns>
433+
/// <remarks><paramref name="fallbackThunk"/> is not evaluated unless <paramref name="source"/> is faulted.</remarks>
434+
///
435+
let inline orElseWith ([<InlineIfLambda>]fallbackThunk: exn -> Task<'T>) (source: Task<'T>) : Task<'T> =
436+
let source = nullArgCheck (nameof source) source
437+
tryWith (fun () -> source) fallbackThunk
438+
439+
/// <summary>Returns <paramref name="source"/> if it is not faulted, otherwise e<paramref name="fallbackTask"/>.</summary>
440+
///
441+
/// <param name="fallbackTask">The alternative Task to use if <paramref name="source"/> is faulted.</param>
442+
/// <param name="source">The input task.</param>
443+
///
444+
/// <returns>The option if the option is Some, else the alternate option.</returns>
445+
let orElse (fallbackTask: Task<'T>) (source: Task<'T>) : Task<'T> =
446+
let fallbackTask = nullArgCheck (nameof fallbackTask) fallbackTask
447+
let source = nullArgCheck (nameof source) source
448+
orElseWith (fun _ -> fallbackTask) source
449+
427450
/// Creates a Task from a value
428451
let result (value: 'T) = Task.FromResult value
429452

src/FSharpPlus/Extensions/ValueTask.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,23 @@ module ValueTask =
295295
(fun () -> if not (isNull (box disp)) then disp.Dispose ())
296296
(fun () -> body disp)
297297

298+
/// <summary>Returns <paramref name="source"/> if it is not faulted, otherwise evaluates <paramref name="fallbackThunk"/> and returns the result.</summary>
299+
///
300+
/// <param name="fallbackThunk">A thunk that provides an alternate task computation when evaluated.</param>
301+
/// <param name="source">The input task.</param>
302+
///
303+
/// <returns>The task if it is not faulted, else the result of evaluating <paramref name="fallbackThunk"/>.</returns>
304+
/// <remarks><paramref name="fallbackThunk"/> is not evaluated unless <paramref name="source"/> is faulted.</remarks>
305+
///
306+
let inline orElseWith ([<InlineIfLambda>]fallbackThunk: exn -> ValueTask<'T>) (source: ValueTask<'T>) : ValueTask<'T> = tryWith fallbackThunk (fun () -> source)
307+
308+
/// <summary>Returns <paramref name="source"/> if it is not faulted, otherwise e<paramref name="fallbackValueTask"/>.</summary>
309+
///
310+
/// <param name="fallbackValueTask">The alternative ValueTask to use if <paramref name="source"/> is faulted.</param>
311+
/// <param name="source">The input task.</param>
312+
///
313+
/// <returns>The option if the option is Some, else the alternate option.</returns>
314+
let orElse (fallbackValueTask: ValueTask<'T>) (source: ValueTask<'T>) : ValueTask<'T> = orElseWith (fun _ -> fallbackValueTask) source
298315

299316
/// Raises an exception in the ValueTask
300317
let raise<'TResult> (``exception``: exn) = ValueTask<'TResult> (Task.FromException<'TResult> ``exception``)

0 commit comments

Comments
 (0)