Skip to content

Commit 71f04ac

Browse files
committed
fixes #176
needed to use the DefaultValue attribute on the Zero member of the CEs
1 parent 13e2d7b commit 71f04ac

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

src/FsToolkit.ErrorHandling.TaskResult/TaskOptionCE.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,19 @@ type TaskOptionBuilderBase() =
514514
TaskOptionCode<'TOverall, 'T>(fun sm -> (generator ()).Invoke(&sm))
515515

516516
/// Used to represent no-ops like the implicit empty "else" branch of an "if" expression.
517-
// [<DefaultValue>] // TODO: Figureout if this attribute is needed, without it it allows tests to pass with implicit else branches resulting in `Some ()`
518-
member inline _.Zero<'TOverall>() : TaskOptionCode<'TOverall, unit> = ResumableCode.Zero()
517+
[<DefaultValue>]
518+
member inline _.Zero<'TOverall>() : TaskOptionCode<'TOverall, unit> =
519+
TaskOptionCode<_, _>
520+
(fun sm ->
521+
sm.Data.Result <- ValueSome(Some Unchecked.defaultof<'TOverall>)
522+
true)
519523

520524
member inline _.Return(value: 'T) : TaskOptionCode<'T, 'T> =
521525
TaskOptionCode<'T, _>
522526
(fun sm ->
523527
sm.Data.Result <- ValueSome(Some value)
524528
true)
525529

526-
527-
528530
static member inline CombineDynamic
529531
(
530532
sm: byref<TaskOptionStateMachine<_>>,

src/FsToolkit.ErrorHandling.TaskResult/TaskResultCE.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ type TaskResultBuilderBase() =
544544
TaskResultCode<'TOverall, 'Error, 'T>(fun sm -> (generator ()).Invoke(&sm))
545545

546546
/// Used to represent no-ops like the implicit empty "else" branch of an "if" expression.
547-
// [<DefaultValue>] // TODO: Figureout if this attribute is needed, without it it allows tests to pass with implicit else branches resulting in `Some ()`
547+
[<DefaultValue>]
548548
member inline _.Zero<'TOverall, 'Error>() : TaskResultCode<'TOverall, 'Error, unit> = ResumableCode.Zero()
549549

550550
member inline _.Return(value: 'T) : TaskResultCode<'T, 'Error, 'T> =

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskOptionCE.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,21 @@ let ceTests =
234234

235235
Expect.equal actual (Some data) "Should be ok"
236236
}
237+
testCaseTask "If do!"
238+
<| fun () ->
239+
task {
240+
let data = 42
241+
242+
let taskRes (call: unit -> Task) maybeCall : Task<Option<int>> =
243+
taskOption {
244+
if true then do! call ()
245+
246+
let! (res: string) = maybeCall (): Task<Option<string>>
247+
return data
248+
}
249+
250+
()
251+
}
237252
testCaseTask "Try With"
238253
<| fun () ->
239254
task {

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResultCE.fs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,24 @@ let ``TaskResultCE combine/zero/delay/run Tests`` =
278278
}
279279

280280
Expect.equal actual (Result.Ok data) "Should be ok"
281-
} ]
281+
}
282+
testCaseTask "If do!"
283+
<| fun () ->
284+
task {
285+
let data = 42
286+
287+
let taskRes (call: unit -> Task) maybeCall : Task<Result<int, unit>> =
288+
taskResult {
289+
if true then do! call ()
290+
291+
let! (res: string) = maybeCall (): Task<Result<string, unit>>
292+
return data
293+
}
294+
295+
()
296+
}
297+
298+
]
282299

283300

284301

0 commit comments

Comments
 (0)