@@ -5,8 +5,6 @@ open System.Threading.Tasks
5
5
6
6
7
7
#if NETSTANDARD2_ 0
8
- open FSharp.Control .Tasks .Affine .Unsafe
9
- open FSharp.Control .Tasks .Affine
10
8
open Ply
11
9
open System.Runtime .CompilerServices
12
10
@@ -15,11 +13,16 @@ module TaskOptionCE =
15
13
type TaskOptionBuilder () =
16
14
member val SomeUnit = Some()
17
15
18
- member inline _.Return ( value : 'T ) : Ply < _ option > = uply.Return <| option.Return value
16
+ member inline _.Return ( value : 'T ) : Ply < _ option > =
17
+ FSharp.Control.Tasks.Affine.Unsafe.uply.Return
18
+ <| option.Return value
19
19
20
- member inline _.ReturnFrom ( taskResult : Task < _ option >) : Ply < _ option > = uply.ReturnFrom taskResult
20
+ member inline _.ReturnFrom ( taskResult : Task < _ option >) : Ply < _ option > =
21
+ FSharp.Control.Tasks.Affine.Unsafe.uply.ReturnFrom taskResult
21
22
22
- member inline _.Zero () : Ply < _ option > = uply.Return <| option.Zero()
23
+ member inline _.Zero () : Ply < _ option > =
24
+ FSharp.Control.Tasks.Affine.Unsafe.uply.Return
25
+ <| option.Zero()
23
26
24
27
member inline _.Bind
25
28
(
@@ -29,18 +32,19 @@ module TaskOptionCE =
29
32
let binder ' r =
30
33
match r with
31
34
| Some x -> binder x
32
- | None -> uply.Return None
35
+ | None -> FSharp.Control.Tasks.Affine.Unsafe. uply.Return None
33
36
34
- uply.Bind( taskResult, binder')
37
+ FSharp.Control.Tasks.Affine.Unsafe. uply.Bind( taskResult, binder')
35
38
36
- member inline _.Delay ( [<InlineIfLambda>] generator : unit -> Ply < _ option >) = uply.Delay( generator)
39
+ member inline _.Delay ( [<InlineIfLambda>] generator : unit -> Ply < _ option >) =
40
+ FSharp.Control.Tasks.Affine.Unsafe.uply.Delay( generator)
37
41
38
42
member inline _.Combine
39
43
(
40
44
computation1 : Ply < 'T option >,
41
45
[<InlineIfLambda>] computation2 : unit -> Ply < 'U option >
42
46
) : Ply < 'U option > =
43
- uply {
47
+ FSharp.Control.Tasks.Affine.Unsafe. uply {
44
48
match ! computation1 with
45
49
| None -> return None
46
50
| Some _ -> return ! computation2 ()
@@ -51,28 +55,28 @@ module TaskOptionCE =
51
55
[<InlineIfLambda>] computation : unit -> Ply < _ option >,
52
56
[<InlineIfLambda>] handler : exn -> Ply < _ option >
53
57
) : Ply < _ option > =
54
- uply.TryWith( computation, handler)
58
+ FSharp.Control.Tasks.Affine.Unsafe. uply.TryWith( computation, handler)
55
59
56
60
member inline _.TryFinally
57
61
(
58
62
[<InlineIfLambda>] computation : unit -> Ply < _ option >,
59
63
[<InlineIfLambda>] compensation : unit -> unit
60
64
) : Ply < _ option > =
61
- uply.TryFinally( computation, compensation)
65
+ FSharp.Control.Tasks.Affine.Unsafe. uply.TryFinally( computation, compensation)
62
66
63
67
member inline _.Using
64
68
(
65
69
resource : 'T :> IDisposable ,
66
70
[<InlineIfLambda>] binder : 'T -> Ply < _ option >
67
71
) : Ply < _ option > =
68
- uply.Using( resource, binder)
72
+ FSharp.Control.Tasks.Affine.Unsafe. uply.Using( resource, binder)
69
73
70
74
member inline _.While
71
75
(
72
76
[<InlineIfLambda>] guard : unit -> bool ,
73
77
[<InlineIfLambda>] computation : unit -> Ply < 'U option >
74
78
) : Ply < 'U option > =
75
- uply {
79
+ FSharp.Control.Tasks.Affine.Unsafe. uply {
76
80
let mutable fin , result = false , None
77
81
78
82
while not fin && guard () do
@@ -86,7 +90,7 @@ module TaskOptionCE =
86
90
}
87
91
88
92
member inline _.For ( sequence : #seq<'T> , binder : 'T -> Ply < 'U option >) : Ply < 'U option > =
89
- uply {
93
+ FSharp.Control.Tasks.Affine.Unsafe. uply {
90
94
use enumerator = sequence.GetEnumerator()
91
95
let mutable fin , result = false , None
92
96
@@ -104,7 +108,7 @@ module TaskOptionCE =
104
108
this.Bind( x, ( fun x -> this.Return( f x)))
105
109
106
110
member inline _.MergeSources ( t1 : Task < 'T option >, t2 : Task < 'T1 option >) = TaskOption.zip t1 t2
107
- member inline _.Run ( [<InlineIfLambda>] f : unit -> Ply < 'm >) = task.Run f
111
+ member inline _.Run ( [<InlineIfLambda>] f : unit -> Ply < 'm >) = FSharp.Control.Tasks.Affine. task.Run f
108
112
109
113
/// <summary>
110
114
/// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
@@ -115,7 +119,8 @@ module TaskOptionCE =
115
119
/// <summary>
116
120
/// Method lets us transform data types into our internal representation.
117
121
/// </summary>
118
- member inline _.Source ( t : ValueTask < _ option >) : Task < _ option > = task { return ! t }
122
+ member inline _.Source ( t : ValueTask < _ option >) : Task < _ option > =
123
+ FSharp.Control.Tasks.Affine.task { return ! t }
119
124
120
125
/// <summary>
121
126
/// Method lets us transform data types into our internal representation.
@@ -125,10 +130,144 @@ module TaskOptionCE =
125
130
/// <summary>
126
131
/// Method lets us transform data types into our internal representation.
127
132
/// </summary>
128
- member inline _.Source ( p : Ply < _ option >) : Task < _ option > = task { return ! p }
133
+ member inline _.Source ( p : Ply < _ option >) : Task < _ option > =
134
+ FSharp.Control.Tasks.Affine.task { return ! p }
129
135
130
136
let taskOption = TaskOptionBuilder()
131
137
138
+ type BackgroundTaskOptionBuilder () =
139
+ member val SomeUnit = Some()
140
+
141
+ member inline _.Return ( value : 'T ) : Ply < _ option > =
142
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.Return
143
+ <| option.Return value
144
+
145
+ member inline _.ReturnFrom ( taskResult : Task < _ option >) : Ply < _ option > =
146
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.ReturnFrom taskResult
147
+
148
+ member inline _.Zero () : Ply < _ option > =
149
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.Return
150
+ <| option.Zero()
151
+
152
+ member inline _.Bind
153
+ (
154
+ taskResult : Task < _ option >,
155
+ [<InlineIfLambda>] binder : 'T -> Ply < _ option >
156
+ ) : Ply < _ option > =
157
+ let binder ' r =
158
+ match r with
159
+ | Some x -> binder x
160
+ | None -> FSharp.Control.Tasks.NonAffine.Unsafe.uply.Return None
161
+
162
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.Bind( taskResult, binder')
163
+
164
+ member inline _.Delay ( [<InlineIfLambda>] generator : unit -> Ply < _ option >) =
165
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.Delay( generator)
166
+
167
+ member inline _.Combine
168
+ (
169
+ computation1 : Ply < 'T option >,
170
+ [<InlineIfLambda>] computation2 : unit -> Ply < 'U option >
171
+ ) : Ply < 'U option > =
172
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply {
173
+ match ! computation1 with
174
+ | None -> return None
175
+ | Some _ -> return ! computation2 ()
176
+ }
177
+
178
+ member inline _.TryWith
179
+ (
180
+ [<InlineIfLambda>] computation : unit -> Ply < _ option >,
181
+ [<InlineIfLambda>] handler : exn -> Ply < _ option >
182
+ ) : Ply < _ option > =
183
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.TryWith( computation, handler)
184
+
185
+ member inline _.TryFinally
186
+ (
187
+ [<InlineIfLambda>] computation : unit -> Ply < _ option >,
188
+ [<InlineIfLambda>] compensation : unit -> unit
189
+ ) : Ply < _ option > =
190
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.TryFinally( computation, compensation)
191
+
192
+ member inline _.Using
193
+ (
194
+ resource : 'T :> IDisposable ,
195
+ [<InlineIfLambda>] binder : 'T -> Ply < _ option >
196
+ ) : Ply < _ option > =
197
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply.Using( resource, binder)
198
+
199
+ member inline _.While
200
+ (
201
+ [<InlineIfLambda>] guard : unit -> bool ,
202
+ [<InlineIfLambda>] computation : unit -> Ply < 'U option >
203
+ ) : Ply < 'U option > =
204
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply {
205
+ let mutable fin , result = false , None
206
+
207
+ while not fin && guard () do
208
+ match ! computation () with
209
+ | Some _ as o -> result <- o
210
+ | None ->
211
+ result <- None
212
+ fin <- true
213
+
214
+ return result
215
+ }
216
+
217
+ member inline _.For ( sequence : #seq<'T> , binder : 'T -> Ply < 'U option >) : Ply < 'U option > =
218
+ FSharp.Control.Tasks.NonAffine.Unsafe.uply {
219
+ use enumerator = sequence.GetEnumerator()
220
+ let mutable fin , result = false , None
221
+
222
+ while not fin && enumerator.MoveNext() do
223
+ match ! binder enumerator.Current with
224
+ | Some _ as o -> result <- o
225
+ | None ->
226
+ result <- None
227
+ fin <- true
228
+
229
+ return result
230
+ }
231
+
232
+ member inline this.BindReturn ( x : Task < 'T option >, [<InlineIfLambda>] f ) =
233
+ this.Bind( x, ( fun x -> this.Return( f x)))
234
+
235
+ member inline _.MergeSources ( t1 : Task < 'T option >, t2 : Task < 'T1 option >) =
236
+ FSharp.Control.Tasks.NonAffine.task {
237
+ let! o1 = t1
238
+ let! o2 = t2
239
+ return Option.zip o1 o2
240
+ }
241
+
242
+ member inline _.Run ( [<InlineIfLambda>] f : unit -> Ply < 'm >) =
243
+ FSharp.Control.Tasks.NonAffine.task.Run f
244
+
245
+ /// <summary>
246
+ /// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
247
+ /// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
248
+ /// </summary>
249
+ member inline _.Source ( task : Task < _ option >) : Task < _ option > = task
250
+
251
+ /// <summary>
252
+ /// Method lets us transform data types into our internal representation.
253
+ /// </summary>
254
+ member inline _.Source ( t : ValueTask < _ option >) : Task < _ option > =
255
+ FSharp.Control.Tasks.NonAffine.task { return ! t }
256
+
257
+ /// <summary>
258
+ /// Method lets us transform data types into our internal representation.
259
+ /// </summary>
260
+ member inline _.Source ( async : Async < _ option >) : Task < _ option > = async |> Async.StartAsTask
261
+
262
+ /// <summary>
263
+ /// Method lets us transform data types into our internal representation.
264
+ /// </summary>
265
+ member inline _.Source ( p : Ply < _ option >) : Task < _ option > =
266
+ FSharp.Control.Tasks.NonAffine.task { return ! p }
267
+
268
+ let backgroundTaskOption = BackgroundTaskOptionBuilder()
269
+
270
+
132
271
133
272
[<AutoOpen>]
134
273
// Having members as extensions gives them lower priority in
@@ -137,7 +276,14 @@ module TaskOptionCEExtensionsLower =
137
276
138
277
type TaskOptionBuilder with
139
278
member inline this.Source ( t : ^TaskLike ) : Task < 'T option > =
140
- task {
279
+ FSharp.Control.Tasks.Affine.task {
280
+ let! r = t
281
+ return Some r
282
+ }
283
+
284
+ type BackgroundTaskOptionBuilder with
285
+ member inline this.Source ( t : ^TaskLike ) : Task < 'T option > =
286
+ FSharp.Control.Tasks.NonAffine.task {
141
287
let! r = t
142
288
return Some r
143
289
}
@@ -161,35 +307,106 @@ module TaskOptionCEExtensions =
161
307
/// <summary>
162
308
/// Method lets us transform data types into our internal representation.
163
309
/// </summary>
164
- member inline _.Source ( a : Task < 'T >) = a |> Task.map Some
310
+ member inline _.Source ( a : Task < 'T >) =
311
+ FSharp.Control.Tasks.Affine.task {
312
+ let! o = a
313
+ return Some o
314
+ }
165
315
166
316
/// <summary>
167
317
/// Method lets us transform data types into our internal representation.
168
318
/// </summary>
169
319
member inline x.Source ( a : Task ) =
170
- task {
320
+ FSharp.Control.Tasks.Affine. task {
171
321
do ! a
172
322
return x.SomeUnit
173
323
}
174
324
175
325
/// <summary>
176
326
/// Method lets us transform data types into our internal representation.
177
327
/// </summary>
178
- member inline _.Source ( a : ValueTask < 'T >) = a |> Task.mapV Some
328
+ member inline _.Source ( a : ValueTask < 'T >) =
329
+ FSharp.Control.Tasks.Affine.task {
330
+ let! o = a
331
+ return Some o
332
+ }
179
333
180
334
/// <summary>
181
335
/// Method lets us transform data types into our internal representation.
182
336
/// </summary>
183
337
member inline x.Source ( a : ValueTask ) =
184
- task {
338
+ FSharp.Control.Tasks.Affine. task {
185
339
do ! a
186
340
return x.SomeUnit
187
341
}
188
342
189
343
/// <summary>
190
344
/// Method lets us transform data types into our internal representation.
191
345
/// </summary>
192
- member inline _.Source ( a : Async < 't >) = a |> Async.StartAsTask |> Task.map Some
346
+ member inline _.Source ( a : Async < 't >) =
347
+ FSharp.Control.Tasks.Affine.task {
348
+ let! o = a |> Async.StartAsTask
349
+ return Some o
350
+ }
351
+
352
+
353
+ type BackgroundTaskOptionBuilder with
354
+ /// <summary>
355
+ /// Needed to allow `for..in` and `for..do` functionality
356
+ /// </summary>
357
+ member inline _.Source ( s : #seq<_> ) = s
358
+
359
+ /// <summary>
360
+ /// Method lets us transform data types into our internal representation.
361
+ /// </summary>
362
+ member inline _.Source ( r : 't option ) = Task.singleton r
363
+
364
+ /// <summary>
365
+ /// Method lets us transform data types into our internal representation.
366
+ /// </summary>
367
+ member inline _.Source ( a : Task < 'T >) =
368
+ FSharp.Control.Tasks.NonAffine.task {
369
+ let! o = a
370
+ return Some o
371
+ }
372
+
373
+ /// <summary>
374
+ /// Method lets us transform data types into our internal representation.
375
+ /// </summary>
376
+ member inline x.Source ( a : Task ) =
377
+ FSharp.Control.Tasks.NonAffine.task {
378
+ do ! a
379
+ return x.SomeUnit
380
+ }
381
+
382
+ /// <summary>
383
+ /// Method lets us transform data types into our internal representation.
384
+ /// </summary>
385
+ member inline _.Source ( a : ValueTask < 'T >) =
386
+ FSharp.Control.Tasks.NonAffine.task {
387
+ let! o = a
388
+ return Some o
389
+ }
390
+
391
+ /// <summary>
392
+ /// Method lets us transform data types into our internal representation.
393
+ /// </summary>
394
+ member inline x.Source ( a : ValueTask ) =
395
+ FSharp.Control.Tasks.NonAffine.task {
396
+ do ! a
397
+ return x.SomeUnit
398
+ }
399
+
400
+ /// <summary>
401
+ /// Method lets us transform data types into our internal representation.
402
+ /// </summary>
403
+ member inline _.Source ( a : Async < 't >) =
404
+ FSharp.Control.Tasks.NonAffine.task {
405
+ let! o = a |> Async.StartAsTask
406
+ return Some o
407
+ }
408
+
409
+
193
410
194
411
#else
195
412
0 commit comments