Skip to content
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

Canonicalize option type annotations #140

Merged
merged 2 commits into from
Oct 19, 2021
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
70 changes: 35 additions & 35 deletions src/FsToolkit.ErrorHandling.JobResult/JobOptionCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ module JobOptionCE =

type JobOptionBuilder() =

member __.Return (value: 'T) : Job<Option<_>> =
member __.Return (value: 'T) : Job<_ option> =
job.Return <| option.Return value

member __.ReturnFrom
(jobResult: Job<Option<_>>)
: Job<Option<_>> =
(jobResult: Job<_ option>)
: Job<_ option> =
jobResult

member __.Zero () : Job<Option<_>> =
member __.Zero () : Job<_ option> =
job.Return <| option.Zero ()

member __.Bind
(jobResult: Job<Option<_>>,
binder: 'T -> Job<Option<_>>)
: Job<Option<_>> =
(jobResult: Job<_ option>,
binder: 'T -> Job<_ option>)
: Job<_ option> =
job {
let! result = jobResult
match result with
Expand All @@ -32,61 +32,61 @@ module JobOptionCE =
}

member this.Bind
(taskResult: unit -> Task<Option<_>>,
binder: 'T -> Job<Option<_>>)
: Job<Option<_>> =
(taskResult: unit -> Task<_ option>,
binder: 'T -> Job<_ option>)
: Job<_ option> =
this.Bind(Job.fromTask taskResult, binder)

member __.Delay
(generator: unit -> Job<Option<_>>)
: Job<Option<_>> =
(generator: unit -> Job<_ option>)
: Job<_ option> =
Job.delay generator

member this.Combine
(computation1: Job<Option<_>>,
computation2: Job<Option<_>>)
: Job<Option<_>> =
(computation1: Job<_ option>,
computation2: Job<_ option>)
: Job<_ option> =
this.Bind(computation1, fun () -> computation2)

member __.TryWith
(computation: Job<Option<_>>,
handler: System.Exception -> Job<Option<_>>)
: Job<Option<_>> =
(computation: Job<_ option>,
handler: System.Exception -> Job<_ option>)
: Job<_ option> =
Job.tryWith computation handler

member __.TryWith
(computation: unit -> Job<Option<_>>,
handler: System.Exception -> Job<Option<_>>)
: Job<Option<_>> =
(computation: unit -> Job<_ option>,
handler: System.Exception -> Job<_ option>)
: Job<_ option> =
Job.tryWithDelay computation handler

member __.TryFinally
(computation: Job<Option<_>>,
(computation: Job<_ option>,
compensation: unit -> unit)
: Job<Option<_>> =
: Job<_ option> =
Job.tryFinallyFun computation compensation

member __.TryFinally
(computation: unit -> Job<Option<_>>,
(computation: unit -> Job<_ option>,
compensation: unit -> unit)
: Job<Option<_>> =
: Job<_ option> =
Job.tryFinallyFunDelay computation compensation

member __.Using
(resource: 'T when 'T :> IDisposable,
binder: 'T -> Job<Option<_>>)
: Job<Option<_>> =
binder: 'T -> Job<_ option>)
: Job<_ option> =
job.Using(resource, binder)

member this.While
(guard: unit -> bool, computation: Job<Option<_>>)
: Job<Option<_>> =
(guard: unit -> bool, computation: Job<_ option>)
: Job<_ option> =
if not <| guard () then this.Zero ()
else this.Bind(computation, fun () -> this.While (guard, computation))

member this.For
(sequence: #seq<'T>, binder: 'T -> Job<Option<_>>)
: Job<Option<_>> =
(sequence: #seq<'T>, binder: 'T -> Job<_ option>)
: Job<_ option> =
this.Using(sequence.GetEnumerator (), fun enum ->
this.While(enum.MoveNext,
this.Delay(fun () -> binder enum.Current)))
Expand All @@ -96,17 +96,17 @@ module JobOptionCE =
///
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
/// </summary>
member inline _.Source(job : Job<Option<_>>) : Job<Option<_>> = job
member inline _.Source(job : Job<_ option>) : Job<_ option> = job

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(async : Async<Option<_>>) : Job<Option<_>> = async |> Job.fromAsync
member inline _.Source(async : Async<_ option>) : Job<_ option> = async |> Job.fromAsync

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(task : Task<Option<_>>) : Job<Option<_>> = task |> Job.awaitTask
member inline _.Source(task : Task<_ option>) : Job<_ option> = task |> Job.awaitTask

let jobOption = JobOptionBuilder()

Expand All @@ -124,7 +124,7 @@ module JobOptionCEExtensions =
/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline __.Source(r: Option<'t>) = Job.singleton r
member inline __.Source(r: 't option) = Job.singleton r
/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
Expand Down
58 changes: 29 additions & 29 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskOptionCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,61 @@ module TaskOptionCE =
member val SomeUnit = Some ()

member inline _.Return (value: 'T)
: Ply<Option<_>> =
: Ply<_ option> =
uply.Return <| option.Return value

member inline _.ReturnFrom
(taskResult: Task<Option<_>>)
: Ply<Option<_>> =
(taskResult: Task<_ option>)
: Ply<_ option> =
uply.ReturnFrom taskResult

member inline _.Zero () : Ply<Option<_>> =
member inline _.Zero () : Ply<_ option> =
uply.Return <| option.Zero()

member inline _.Bind
(taskResult: Task<Option<_>>,
binder: 'T -> Ply<Option<_>>)
: Ply<Option<_>> =
(taskResult: Task<_ option>,
binder: 'T -> Ply<_ option>)
: Ply<_ option> =
let binder' r =
match r with
| Some x -> binder x
| None -> uply.Return None
uply.Bind(taskResult, binder')

member inline _.Delay
(generator: unit -> Ply<Option<_>>) =
(generator: unit -> Ply<_ option>) =
uply.Delay(generator)

member inline _.Combine
(computation1: Ply<Option<'T>>,
computation2: unit -> Ply<Option<'U>>)
: Ply<Option<'U>> = uply {
(computation1: Ply<'T option>,
computation2: unit -> Ply<'U option>)
: Ply<'U option> = uply {
match! computation1 with
| None -> return None
| Some _ -> return! computation2()
}

member inline _.TryWith
(computation: unit -> Ply<Option<_>>,
handler: exn -> Ply<Option<_>>) :
Ply<Option<_>> =
(computation: unit -> Ply<_ option>,
handler: exn -> Ply<_ option>) :
Ply<_ option> =
uply.TryWith(computation, handler)

member inline _.TryFinally
(computation: unit -> Ply<Option<_>>,
(computation: unit -> Ply<_ option>,
compensation: unit -> unit)
: Ply<Option<_>> =
: Ply<_ option> =
uply.TryFinally(computation, compensation)

member inline _.Using
(resource: 'T when 'T :> IDisposable,
binder: 'T -> Ply<Option<_>>)
: Ply<Option<_>> =
binder: 'T -> Ply<_ option>)
: Ply<_ option> =
uply.Using(resource, binder)

member _.While
(guard: unit -> bool, computation: unit -> Ply<Option<'U>>)
: Ply<Option<'U>> = uply {
(guard: unit -> bool, computation: unit -> Ply<'U option>)
: Ply<'U option> = uply {
let mutable fin, result = false, None
while not fin && guard() do
match! computation() with
Expand All @@ -79,8 +79,8 @@ module TaskOptionCE =
}

member _.For
(sequence: #seq<'T>, binder: 'T -> Ply<Option<'U>>)
: Ply<Option<'U>> = uply {
(sequence: #seq<'T>, binder: 'T -> Ply<'U option>)
: Ply<'U option> = uply {
use enumerator = sequence.GetEnumerator()
let mutable fin, result = false, None
while not fin && enumerator.MoveNext() do
Expand All @@ -93,30 +93,30 @@ module TaskOptionCE =
return result
}

member inline this.BindReturn(x: Task<Option<'T>>, f) = this.Bind(x, fun x -> this.Return(f x))
member inline _.MergeSources(t1: Task<Option<'T>>, t2: Task<Option<'T1>>) = TaskOption.zip t1 t2
member inline this.BindReturn(x: Task<'T option>, f) = this.Bind(x, fun x -> this.Return(f x))
member inline _.MergeSources(t1: Task<'T option>, t2: Task<'T1 option>) = TaskOption.zip t1 t2
member inline _.Run(f : unit -> Ply<'m>) = task.Run f

/// <summary>
/// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
/// </summary>
member inline _.Source(task : Task<Option<_>>) : Task<Option<_>> = task
member inline _.Source(task : Task<_ option>) : Task<_ option> = task

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(t : ValueTask<Option<_>>) : Task<Option<_>> = task { return! t }
member inline _.Source(t : ValueTask<_ option>) : Task<_ option> = task { return! t }

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(async : Async<Option<_>>) : Task<Option<_>> = async |> Async.StartAsTask
member inline _.Source(async : Async<_ option>) : Task<_ option> = async |> Async.StartAsTask

/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline _.Source(p : Ply<Option<_>>) : Task<Option<_>> = task { return! p }
member inline _.Source(p : Ply<_ option>) : Task<_ option> = task { return! p }

let taskOption = TaskOptionBuilder()

Expand All @@ -134,7 +134,7 @@ module TaskOptionCEExtensions =
/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>
member inline __.Source(r: Option<'t>) = Task.singleton r
member inline __.Source(r: 't option) = Task.singleton r

/// <summary>
/// Method lets us transform data types into our internal representation.
Expand Down
Loading