@@ -7,9 +7,8 @@ open FSharp.Compiler.SourceCodeServices
7
7
[<TestFixture>]
8
8
module ComputationExpressionTests =
9
9
10
- [<Test>]
11
- let ``do - bang can be used with nested CE expressions`` () =
12
- let code = """
10
+ let ``complex CE with source member and applicatives`` ceUsage =
11
+ sprintf """
13
12
module Code
14
13
type ResultBuilder() =
15
14
member __.Return value = Ok value
@@ -29,10 +28,12 @@ module Result =
29
28
| Ok x1res, Ok x2res -> Ok (x1res, x2res)
30
29
| Error e, _ -> Error e
31
30
| _, Error e -> Error e
31
+
32
32
let ofChoice c =
33
33
match c with
34
34
| Choice1Of2 x -> Ok x
35
35
| Choice2Of2 x -> Error x
36
+
36
37
let fold onOk onError r =
37
38
match r with
38
39
| Ok x -> onOk x
@@ -49,9 +50,10 @@ module Async =
49
50
}
50
51
51
52
module AsyncResult =
52
- let zip x1 x2 =
53
+ let zip x1 x2 =
53
54
Async.zip x1 x2
54
55
|> Async.map(fun (r1, r2) -> Result.zip r1 r2)
56
+
55
57
let foldResult onSuccess onError ar =
56
58
Async.map (Result.fold onSuccess onError) ar
57
59
@@ -101,7 +103,7 @@ type AsyncResultBuilder() =
101
103
compensation: unit -> unit)
102
104
: Async<Result<'T, 'TError>> =
103
105
async.TryFinally(computation, compensation)
104
-
106
+
105
107
member __.Using
106
108
(resource: 'T when 'T :> System.IDisposable,
107
109
binder: 'T -> Async<Result<'U, 'TError>>)
@@ -127,6 +129,7 @@ type AsyncResultBuilder() =
127
129
128
130
member inline _.Source(result : Async<Result<_,_>>) : Async<Result<_,_>> = result
129
131
132
+ [<AutoOpen>]
130
133
module ARExts =
131
134
type AsyncResultBuilder with
132
135
/// <summary>
@@ -151,9 +154,14 @@ module ARExts =
151
154
/// Method lets us transform data types into our internal representation.
152
155
/// </summary>
153
156
member inline __.Source(asyncComputation : Async<_>) : Async<Result<_,_>> = asyncComputation |> Async.map Ok
154
-
157
+
155
158
let asyncResult = AsyncResultBuilder()
156
159
160
+ %s """ ceUsage
161
+
162
+ [<Test>]
163
+ let ``do - bang can be used with nested CE expressions`` () =
164
+ let code = `` complex CE with source member and applicatives `` """
157
165
asyncResult {
158
166
let! something = asyncResult { return 5 }
159
167
do! asyncResult {
@@ -165,4 +173,21 @@ asyncResult {
165
173
|> Async.RunSynchronously
166
174
|> printfn "%d "
167
175
"""
168
- CompilerAssert.Pass code
176
+ CompilerAssert.Pass code
177
+
178
+ [<Test>]
179
+ let ``match - bang should apply source transformations to its inputs`` () =
180
+ let code = `` complex CE with source member and applicatives `` """
181
+ asyncResult {
182
+ // if the source transformation is not applied, the match will not work,
183
+ // because match! is only defined in terms of let!, and the only
184
+ // bind overload provided takes AsyncResult as its input.
185
+ match! Ok 5 with
186
+ | 5 -> return "ok"
187
+ | n -> return! (Error (sprintf "boo %d " n))
188
+ }
189
+ |> AsyncResult.foldResult id (fun (err: string) -> err)
190
+ |> Async.RunSynchronously
191
+ |> printfn "%s "
192
+ """
193
+ CompilerAssert.Pass code
0 commit comments