Skip to content

Commit

Permalink
fixes #176
Browse files Browse the repository at this point in the history
needed to use the DefaultValue attribute on the Zero member of the CEs
  • Loading branch information
TheAngryByrd committed Apr 15, 2022
1 parent 13e2d7b commit c39d071
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskOptionCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,12 @@ type TaskOptionBuilderBase() =
TaskOptionCode<'TOverall, 'T>(fun sm -> (generator ()).Invoke(&sm))

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

member inline _.Return(value: 'T) : TaskOptionCode<'T, 'T> =
TaskOptionCode<'T, _>
Expand Down
2 changes: 1 addition & 1 deletion src/FsToolkit.ErrorHandling.TaskResult/TaskResultCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ type TaskResultBuilderBase() =
TaskResultCode<'TOverall, 'Error, 'T>(fun sm -> (generator ()).Invoke(&sm))

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

member inline _.Return(value: 'T) : TaskResultCode<'T, 'Error, 'T> =
Expand Down
11 changes: 11 additions & 0 deletions tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskOptionCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ let ceTests =

Expect.equal actual (Some data) "Should be ok"
}
testCaseTask "If do!" <| fun () -> task {
let data = 42

let taskRes (call : unit -> Task) maybeCall : Task<Option<int>>= taskOption {
if true then
do! call ()

let! (res: string) = maybeCall (): Task<Option<string>>
return data }
()
}
testCaseTask "Try With"
<| fun () ->
task {
Expand Down
15 changes: 14 additions & 1 deletion tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResultCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,20 @@ let ``TaskResultCE combine/zero/delay/run Tests`` =
}

Expect.equal actual (Result.Ok data) "Should be ok"
} ]
}
testCaseTask "If do!" <| fun () -> task {
let data = 42

let taskRes (call : unit -> Task) maybeCall : Task<Result<int,unit>>= taskResult {
if true then
do! call ()

let! (res: string) = maybeCall (): Task<Result<string, unit>>
return data }
()
}

]



Expand Down

0 comments on commit c39d071

Please sign in to comment.