Skip to content

Commit 3e95bc5

Browse files
committed
Unifying generic type names, removing redundant annotations and more consistency in code
1 parent 6be76b0 commit 3e95bc5

File tree

2 files changed

+54
-69
lines changed

2 files changed

+54
-69
lines changed

src/FSharp.Control.TaskSeq/TaskSeqBuilder.fs

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ and TaskSeqResumptionDynamicInfo<'T> = ResumptionDynamicInfo<TaskSeqStateMachine
307307

308308
type TaskSeqBuilder() =
309309

310-
member inline _.Delay(f: unit -> ResumableTSC<'T>) : ResumableTSC<'T> = ResumableTSC<'T>(fun sm -> f().Invoke(&sm))
310+
member inline _.Delay(f: unit -> ResumableTSC<'T>) = ResumableTSC<'T>(fun sm -> f().Invoke(&sm))
311311

312312
member inline _.Run(code: ResumableTSC<'T>) : IAsyncEnumerable<'T> =
313313
if __useResumableCode then
@@ -348,11 +348,9 @@ type TaskSeqBuilder() =
348348
// but only a reference to itself.
349349
let boxed = sm.Data.boxedSelf
350350

351-
sm.Data.awaiter.UnsafeOnCompleted(
352-
Action(fun () ->
353-
let mutable boxed = boxed
354-
moveNextRef &boxed)
355-
)
351+
sm.Data.awaiter.UnsafeOnCompleted(fun () ->
352+
let mutable boxed = boxed
353+
moveNextRef &boxed)
356354

357355
with exn ->
358356
Debug.logInfo ("Setting exception of PromiseOfValueOrEnd to: ", exn.Message)
@@ -386,7 +384,7 @@ type TaskSeqBuilder() =
386384
Debug.logInfo "at Zero()"
387385
ResumableCode.Zero()
388386

389-
member inline _.Combine(task1: ResumableTSC<'T>, task2: ResumableTSC<'T>) : ResumableTSC<'T> =
387+
member inline _.Combine(task1: ResumableTSC<'T>, task2: ResumableTSC<'T>) =
390388
Debug.logInfo "at Combine(.., ..)"
391389

392390
ResumableCode.Combine(task1, task2)
@@ -401,7 +399,7 @@ type TaskSeqBuilder() =
401399

402400
ResumableCode.While(
403401
(fun () -> condition_res),
404-
ResumableCode<_, _>(fun sm ->
402+
ResumableTSC<'T>(fun sm ->
405403
let mutable __stack_condition_fin = true
406404
let __stack_vtask = condition ()
407405

@@ -432,25 +430,24 @@ type TaskSeqBuilder() =
432430
false)
433431
)
434432

435-
member inline b.While([<InlineIfLambda>] condition: unit -> bool, body: ResumableTSC<'T>) : ResumableTSC<'T> =
433+
member inline _.While([<InlineIfLambda>] condition: unit -> bool, body: ResumableTSC<'T>) =
436434
Debug.logInfo "at While(...)"
437435
ResumableCode.While(condition, body)
438436

439-
member inline _.TryWith(body: ResumableTSC<'T>, catch: exn -> ResumableTSC<'T>) : ResumableTSC<'T> =
440-
ResumableCode.TryWith(body, catch)
437+
member inline _.TryWith(body: ResumableTSC<'T>, catch: exn -> ResumableTSC<'T>) = ResumableCode.TryWith(body, catch)
441438

442-
member inline _.TryFinallyAsync(body: ResumableTSC<'T>, compensation: unit -> Task) : ResumableTSC<'T> =
439+
member inline _.TryFinallyAsync(body: ResumableTSC<'T>, compensationAction: unit -> Task) =
443440
ResumableCode.TryFinallyAsync(
444441

445442
ResumableTSC<'T>(fun sm ->
446-
sm.Data.PushDispose(fun () -> compensation ())
443+
sm.Data.PushDispose compensationAction
447444
body.Invoke(&sm)),
448445

449-
ResumableCode<_, _>(fun sm ->
446+
ResumableTSC<'T>(fun sm ->
450447

451448
sm.Data.PopDispose()
452449
let mutable __stack_condition_fin = true
453-
let __stack_vtask = compensation ()
450+
let __stack_vtask = compensationAction ()
454451

455452
if not __stack_vtask.IsCompleted then
456453
let mutable awaiter = __stack_vtask.GetAwaiter()
@@ -463,22 +460,19 @@ type TaskSeqBuilder() =
463460
__stack_condition_fin)
464461
)
465462

466-
member inline _.TryFinally(body: ResumableTSC<'T>, compensation: unit -> unit) : ResumableTSC<'T> =
463+
member inline _.TryFinally(body: ResumableTSC<'T>, compensationAction: unit -> unit) =
467464
ResumableCode.TryFinally(
468465
ResumableTSC<'T>(fun sm ->
469-
sm.Data.PushDispose(fun () ->
470-
compensation ()
471-
Task.CompletedTask)
472-
466+
sm.Data.PushDispose(compensationAction >> Task.get_CompletedTask)
473467
body.Invoke(&sm)),
474468

475-
ResumableCode<_, _>(fun sm ->
469+
ResumableTSC<'T>(fun sm ->
476470
sm.Data.PopDispose()
477-
compensation ()
471+
compensationAction ()
478472
true)
479473
)
480474

481-
member inline this.Using(disp: #IAsyncDisposable, body: #IAsyncDisposable -> ResumableTSC<'T>) : ResumableTSC<'T> =
475+
member inline this.Using(disp: #IAsyncDisposable, body: #IAsyncDisposable -> ResumableTSC<'T>) =
482476

483477
// A using statement is just a try/finally with the finally block disposing if non-null.
484478
this.TryFinallyAsync(
@@ -490,14 +484,14 @@ type TaskSeqBuilder() =
490484
Task.CompletedTask)
491485
)
492486

493-
member inline _.Yield(v: 'T) : ResumableTSC<'T> =
487+
member inline _.Yield(value: 'T) : ResumableTSC<'T> =
494488
ResumableTSC<'T>(fun sm ->
495489
// This will yield with __stack_fin = false
496490
// This will resume with __stack_fin = true
497491
Debug.logInfo "at Yield"
498492

499493
let __stack_fin = ResumableCode.Yield().Invoke(&sm)
500-
sm.Data.current <- ValueSome v
494+
sm.Data.current <- ValueSome value
501495
sm.Data.awaiter <- null
502496
__stack_fin)
503497

@@ -535,34 +529,33 @@ module LowPriority =
535529
// - Task<'T> (because it only implements GetResult() -> unit, not GetResult() -> 'TResult)
536530

537531
[<NoEagerConstraintApplication>]
538-
member inline _.Bind< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall
532+
member inline _.Bind< ^TaskLike, 'T, 'U, ^Awaiter, 'TOverall
539533
when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
540534
and ^Awaiter :> ICriticalNotifyCompletion
541535
and ^Awaiter: (member get_IsCompleted: unit -> bool)
542-
and ^Awaiter: (member GetResult: unit -> 'TResult1)>
536+
and ^Awaiter: (member GetResult: unit -> 'T)>
543537
(
544538
task: ^TaskLike,
545-
continuation: ('TResult1 -> ResumableTSC<'TResult2>)
546-
) : ResumableTSC<'TResult2> =
539+
continuation: ('T -> ResumableTSC<'U>)
540+
) =
547541

548-
ResumableTSC<'TResult2>(fun sm ->
542+
ResumableTSC<'U>(fun sm ->
549543
let mutable awaiter = (^TaskLike: (member GetAwaiter: unit -> ^Awaiter) (task))
550544
let mutable __stack_fin = true
551545

552546
Debug.logInfo "at TaskLike bind"
553547

554-
if not (^Awaiter: (member get_IsCompleted: unit -> bool) (awaiter)) then
548+
if not (^Awaiter: (member get_IsCompleted: unit -> bool) awaiter) then
555549
// This will yield with __stack_fin2 = false
556550
// This will resume with __stack_fin2 = true
557551
let __stack_fin2 = ResumableCode.Yield().Invoke(&sm)
558552
__stack_fin <- __stack_fin2
559553

560-
Debug.logInfo ("at TaskLike bind: with __stack_fin = ", __stack_fin)
561554
Debug.logInfo ("at TaskLike bind: this.completed = ", sm.Data.completed)
562555

563556
if __stack_fin then
564557
Debug.logInfo "at TaskLike bind!: finished awaiting, calling continuation"
565-
let result = (^Awaiter: (member GetResult: unit -> 'TResult1) (awaiter))
558+
let result = (^Awaiter: (member GetResult: unit -> 'T) awaiter)
566559
(continuation result).Invoke(&sm)
567560

568561
else
@@ -577,43 +570,37 @@ module LowPriority =
577570
module MediumPriority =
578571
type TaskSeqBuilder with
579572

580-
member inline this.Using(disp: #IDisposable, body: #IDisposable -> ResumableTSC<'T>) : ResumableTSC<'T> =
573+
member inline this.Using(dispensation: #IDisposable, body: #IDisposable -> ResumableTSC<'T>) =
581574

582575
// A using statement is just a try/finally with the finally block disposing if non-null.
583576
this.TryFinally(
584-
(fun sm -> (body disp).Invoke(&sm)),
577+
(fun sm -> (body dispensation).Invoke(&sm)),
585578
(fun () ->
586579
// yes, this can be null from time to time
587-
if not (isNull (box disp)) then
588-
disp.Dispose())
580+
if not (isNull (box dispensation)) then
581+
dispensation.Dispose())
589582
)
590583

591-
member inline this.For(sequence: seq<'TElement>, body: 'TElement -> ResumableTSC<'T>) : ResumableTSC<'T> =
584+
member inline this.For(sequence: seq<'TElement>, body: 'TElement -> ResumableTSC<'T>) =
592585
// A for loop is just a using statement on the sequence's enumerator...
593586
this.Using(
594587
sequence.GetEnumerator(),
595588
// ... and its body is a while loop that advances the enumerator and runs the body on each element.
596-
(fun e -> this.While((fun () -> e.MoveNext()), (fun sm -> (body e.Current).Invoke(&sm))))
589+
fun e -> this.While(e.MoveNext, (fun sm -> (body e.Current).Invoke(&sm)))
597590
)
598591

599-
member inline this.YieldFrom(source: seq<'T>) : ResumableTSC<'T> = this.For(source, (fun v -> this.Yield(v)))
592+
member inline this.YieldFrom(source: seq<'T>) : ResumableTSC<'T> = this.For(source, this.Yield)
600593

601-
member inline this.For
602-
(
603-
source: #IAsyncEnumerable<'TElement>,
604-
body: 'TElement -> ResumableTSC<'T>
605-
) : ResumableTSC<'T> =
594+
member inline this.For(source: #IAsyncEnumerable<'TElement>, body: 'TElement -> ResumableTSC<'T>) =
606595
ResumableTSC<'T>(fun sm ->
607596
this
608597
.Using(
609598
source.GetAsyncEnumerator(sm.Data.cancellationToken),
610-
(fun e ->
611-
this.WhileAsync((fun () -> e.MoveNextAsync()), (fun sm -> (body e.Current).Invoke(&sm))))
599+
fun e -> this.WhileAsync(e.MoveNextAsync, (fun sm -> (body e.Current).Invoke(&sm)))
612600
)
613601
.Invoke(&sm))
614602

615-
member inline this.YieldFrom(source: IAsyncEnumerable<'T>) : ResumableTSC<'T> =
616-
this.For(source, (fun v -> this.Yield(v)))
603+
member inline this.YieldFrom(source: IAsyncEnumerable<'T>) = this.For(source, (fun v -> this.Yield(v)))
617604

618605
[<AutoOpen>]
619606
module HighPriority =
@@ -629,8 +616,8 @@ module HighPriority =
629616
// - In contrast, ValueTask<_> *does have* GetResult() -> 'TResult
630617
// - Conclusion: we do not need an extra overload anymore for ValueTask
631618
//
632-
member inline _.Bind(task: Task<'TResult1>, continuation: ('TResult1 -> ResumableTSC<'T>)) : ResumableTSC<'T> =
633-
ResumableTSC<'T>(fun sm ->
619+
member inline _.Bind(task: Task<'T>, continuation: ('T -> ResumableTSC<'U>)) =
620+
ResumableTSC<'U>(fun sm ->
634621
let mutable awaiter = task.GetAwaiter()
635622
let mutable __stack_fin = true
636623

@@ -657,15 +644,11 @@ module HighPriority =
657644
sm.Data.current <- ValueNone
658645
false)
659646

660-
member inline _.Bind
661-
(
662-
asyncSource: Async<'TResult1>,
663-
continuation: ('TResult1 -> ResumableTSC<'T>)
664-
) : ResumableTSC<'T> =
665-
ResumableTSC<'T>(fun sm ->
647+
member inline _.Bind(computation: Async<'T>, continuation: ('T -> ResumableTSC<'U>)) =
648+
ResumableTSC<'U>(fun sm ->
666649
let mutable awaiter =
667650
Async
668-
.StartAsTask(asyncSource, cancellationToken = sm.Data.cancellationToken)
651+
.StartAsTask(computation, cancellationToken = sm.Data.cancellationToken)
669652
.GetAwaiter()
670653

671654
let mutable __stack_fin = true

src/FSharp.Control.TaskSeq/TaskSeqBuilder.fsi

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@ type TaskSeqBuilder =
131131
member inline Combine: task1: ResumableTSC<'T> * task2: ResumableTSC<'T> -> ResumableTSC<'T>
132132
member inline Delay: f: (unit -> ResumableTSC<'T>) -> ResumableTSC<'T>
133133
member inline Run: code: ResumableTSC<'T> -> taskSeq<'T>
134-
member inline TryFinally: body: ResumableTSC<'T> * compensation: (unit -> unit) -> ResumableTSC<'T>
135-
member inline TryFinallyAsync: body: ResumableTSC<'T> * compensation: (unit -> Task) -> ResumableTSC<'T>
134+
member inline TryFinally: body: ResumableTSC<'T> * compensationAction: (unit -> unit) -> ResumableTSC<'T>
135+
member inline TryFinallyAsync: body: ResumableTSC<'T> * compensationAction: (unit -> Task) -> ResumableTSC<'T>
136136
member inline TryWith: body: ResumableTSC<'T> * catch: (exn -> ResumableTSC<'T>) -> ResumableTSC<'T>
137-
member inline Using: disp: 'a * body: ('a -> ResumableTSC<'T>) -> ResumableTSC<'T> when 'a :> IAsyncDisposable
137+
member inline Using:
138+
disp: 'Disp * body: ('Disp -> ResumableTSC<'T>) -> ResumableTSC<'T> when 'Disp :> IAsyncDisposable
138139
member inline While: condition: (unit -> bool) * body: ResumableTSC<'T> -> ResumableTSC<'T>
139140
/// Used by `For`. F# currently doesn't support `while!`, so this cannot be called directly from the CE
140141
member inline WhileAsync: condition: (unit -> ValueTask<bool>) * body: ResumableTSC<'T> -> ResumableTSC<'T>
141-
member inline Yield: v: 'T -> ResumableTSC<'T>
142+
member inline Yield: value: 'T -> ResumableTSC<'T>
142143
member inline Zero: unit -> ResumableTSC<'T>
143144

144145
[<AutoOpen>]
@@ -159,12 +160,12 @@ module LowPriority =
159160
type TaskSeqBuilder with
160161

161162
[<NoEagerConstraintApplication>]
162-
member inline Bind< ^TaskLike, 'TResult1, 'TResult2, ^Awaiter, 'TOverall> :
163-
task: ^TaskLike * continuation: ('TResult1 -> ResumableTSC<'TResult2>) -> ResumableTSC<'TResult2>
163+
member inline Bind< ^TaskLike, 'T, 'U, ^Awaiter, 'TOverall> :
164+
task: ^TaskLike * continuation: ('T -> ResumableTSC<'U>) -> ResumableTSC<'U>
164165
when ^TaskLike: (member GetAwaiter: unit -> ^Awaiter)
165166
and ^Awaiter :> ICriticalNotifyCompletion
166167
and ^Awaiter: (member get_IsCompleted: unit -> bool)
167-
and ^Awaiter: (member GetResult: unit -> 'TResult1)
168+
and ^Awaiter: (member GetResult: unit -> 'T)
168169

169170
/// <summary>
170171
/// Contains low priority extension methods for the main builder class for the <see cref="taskSeq" /> computation expression.
@@ -175,7 +176,9 @@ module LowPriority =
175176
module MediumPriority =
176177
type TaskSeqBuilder with
177178

178-
member inline Using: disp: 'a * body: ('a -> ResumableTSC<'T>) -> ResumableTSC<'T> when 'a :> IDisposable
179+
// NOTE: syntax with '#Disposable' won't work properly in FSI
180+
member inline Using:
181+
dispensation: 'Disp * body: ('Disp -> ResumableTSC<'T>) -> ResumableTSC<'T> when 'Disp :> IDisposable
179182
member inline For: sequence: seq<'TElement> * body: ('TElement -> ResumableTSC<'T>) -> ResumableTSC<'T>
180183
member inline YieldFrom: source: seq<'T> -> ResumableTSC<'T>
181184
member inline For: source: #taskSeq<'TElement> * body: ('TElement -> ResumableTSC<'T>) -> ResumableTSC<'T>
@@ -190,6 +193,5 @@ module MediumPriority =
190193
module HighPriority =
191194
type TaskSeqBuilder with
192195

193-
member inline Bind: task: Task<'TResult1> * continuation: ('TResult1 -> ResumableTSC<'T>) -> ResumableTSC<'T>
194-
member inline Bind:
195-
asyncSource: Async<'TResult1> * continuation: ('TResult1 -> ResumableTSC<'T>) -> ResumableTSC<'T>
196+
member inline Bind: task: Task<'T> * continuation: ('T -> ResumableTSC<'U>) -> ResumableTSC<'U>
197+
member inline Bind: computation: Async<'T> * continuation: ('T -> ResumableTSC<'U>) -> ResumableTSC<'U>

0 commit comments

Comments
 (0)