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 71f04ac
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskOptionCE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -514,17 +514,19 @@ 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>]

This comment has been minimized.

Copy link
@kerams

kerams Apr 16, 2022

Contributor

Guess we've figured it out :P. Much obliged.

This comment has been minimized.

Copy link
@TheAngryByrd

TheAngryByrd Apr 16, 2022

Author Collaborator

Yeah I hope this implementation for taskOption works out. Please open another issue if this doesn't.

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, _>
(fun sm ->
sm.Data.Result <- ValueSome(Some value)
true)



static member inline CombineDynamic
(
sm: byref<TaskOptionStateMachine<_>>,
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
15 changes: 15 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,21 @@ 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
19 changes: 18 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,24 @@ 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 71f04ac

Please sign in to comment.