Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 870
feat(Async+Task+ValueTask): consistent helper modules#19844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a21b6c9d2e4b46bc8317040245a26708b8fbb5711160f399ecfec168ac1ad45c76dc2067f7cc185226b57e9ef3ff6c0473bdaf36cca111e73672447810c0f53af19dd5cc18832ea23273c2e2ffa28f3a37d67ac86ab04cb460ea03e1711e4f086f0315b19bac60bfc376f6d767c6a3fc80846fb9dfbda8b29f3ecca95c0036626b9f34f4413c73249e732ab276ffcb7a414492f7e7a991115e1d2a59b0bf8d194ecf04b9b0c6debf1b966b35f59e11d4a75dbea376f425758899ede598a719828File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -716,3 +716,154 @@ module LowPlusPriority = | ||
| this.Bind(computation, fun (result2: ^TResult2) -> this.Return struct (result1, result2)) | ||
| ) | ||
| ) | ||
| namespace Microsoft.FSharp.Control | ||
| open System.Threading.Tasks | ||
| open Microsoft.FSharp.Core | ||
| open TaskBuilder | ||
| open Microsoft.FSharp.Control.TaskBuilderExtensions | ||
| open Microsoft.FSharp.Control.TaskBuilderExtensions.LowPriority | ||
| open Microsoft.FSharp.Control.TaskBuilderExtensions.HighPriority | ||
| [<RequireQualifiedAccess>] | ||
| [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] | ||
| module Task = | ||
| [<CompiledName("Result")>] | ||
| let inline result (value: 'T) : Task<'T> = | ||
| Task.FromResult value | ||
| [<CompiledName("Empty")>] | ||
| let empty: Task<unit> = result () | ||
| [<CompiledName("Bind")>] | ||
| let inline bind ([<InlineIfLambda>] binder: 'T -> Task<'U>) (task: Task<'T>) : Task<'U> = | ||
| if task.Status = TaskStatus.RanToCompletion then | ||
| try | ||
| binder task.Result | ||
| with e -> | ||
| Task.FromException<'U>(e) | ||
| else | ||
| TaskBuilder.task { | ||
| let! v = task | ||
| return! binder v | ||
| } | ||
| [<CompiledName("Map")>] | ||
| let inline map ([<InlineIfLambda>] mapping: 'T -> 'U) (task: Task<'T>) : Task<'U> = | ||
| if task.Status = TaskStatus.RanToCompletion then | ||
| try | ||
| mapping task.Result |> result | ||
| with e -> | ||
| Task.FromException<'U>(e) | ||
| else | ||
| TaskBuilder.task { | ||
| let! v = task | ||
| return mapping v | ||
| } | ||
| [<CompiledName("Ignore")>] | ||
| [<RequiresExplicitTypeArguments>] | ||
| let inline ignore<'T> (task: Task<'T>) : Task<unit> = | ||
bartelink marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if task.Status = TaskStatus.RanToCompletion then | ||
| empty | ||
| else | ||
| map ignore task | ||
| [<CompiledName("CatchWith")>] | ||
| let inline catchWith ([<InlineIfLambda>] handler: exn -> 'T) (task: Task<'T>) : Task<'T> = | ||
bartelink marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if task.Status = TaskStatus.RanToCompletion then | ||
| task | ||
| else | ||
| TaskBuilder.task { | ||
| try | ||
| return! task | ||
| with | ||
| | :? System.OperationCanceledException as e -> return! raise e | ||
| | e -> return handler e | ||
| } | ||
| [<CompiledName("Catch")>] | ||
bartelink marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| let catch (task: Task<'T>) : Task<Result<'T, exn>> = | ||
bartelink marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| task |> map Ok |> catchWith Error | ||
| #if NETSTANDARD2_1 | ||
| [<CompiledName("OfValueTask")>] | ||
| let inline ofValueTask (valueTask: ValueTask<'T>) : Task<'T> = | ||
| valueTask.AsTask() | ||
| #endif | ||
| #if NETSTANDARD2_1 | ||
| [<RequireQualifiedAccess>] | ||
| [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] | ||
| module ValueTask = | ||
| [<CompiledName("Result")>] | ||
| let inline result (value: 'T) : ValueTask<'T> = | ||
| ValueTask<'T>(value) | ||
| [<CompiledName("Empty")>] | ||
| let empty: ValueTask<unit> = result () | ||
| [<CompiledName("OfTask")>] | ||
| let inline ofTask (task: Task<'T>) : ValueTask<'T> = | ||
| ValueTask<'T>(task) | ||
| [<CompiledName("Bind")>] | ||
| let inline bind ([<InlineIfLambda>] binder: 'T -> ValueTask<'U>) (task: ValueTask<'T>) : ValueTask<'U> = | ||
| if task.IsCompletedSuccessfully then | ||
| try | ||
| binder task.Result | ||
| with e -> | ||
| Task.FromException<'U>(e) |> ofTask | ||
| else | ||
| let t: Task<'U> = | ||
| TaskBuilder.task { | ||
| let! v = task | ||
| return! binder v | ||
| } | ||
| ValueTask<'U>(t) | ||
| [<CompiledName("Map")>] | ||
| let inline map ([<InlineIfLambda>] mapping: 'T -> 'U) (task: ValueTask<'T>) : ValueTask<'U> = | ||
| if task.IsCompletedSuccessfully then | ||
| try | ||
| mapping task.Result |> result | ||
| with e -> | ||
| Task.FromException<'U>(e) |> ofTask | ||
| else | ||
| let t: Task<'U> = | ||
| TaskBuilder.task { | ||
| let! v = task | ||
| return mapping v | ||
| } | ||
| ValueTask<'U>(t) | ||
| [<CompiledName("Ignore")>] | ||
| [<RequiresExplicitTypeArguments>] | ||
| let inline ignore<'T> (task: ValueTask<'T>) : ValueTask<unit> = | ||
bartelink marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| map ignore task | ||
| [<CompiledName("CatchWith")>] | ||
| let inline catchWith ([<InlineIfLambda>] handler: exn -> 'T) (task: ValueTask<'T>) : ValueTask<'T> = | ||
| if task.IsCompletedSuccessfully then | ||
| task | ||
| else | ||
| let t: Task<'T> = | ||
| TaskBuilder.task { | ||
| try | ||
| return! task | ||
| with | ||
| | :? System.OperationCanceledException as e -> return! raise e | ||
| | e -> return handler e | ||
| } | ||
| ValueTask<'T>(t) | ||
| [<CompiledName("Catch")>] | ||
| let catch (task: ValueTask<'T>) : ValueTask<Result<'T, exn>> = | ||
| task |> map Ok |> catchWith Error | ||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.