demystifyfp/FsToolkit.ErrorHandling

TaskResultOption compatibility with TaskResult and friends

jozefRudy opened this issue · 4 comments

Is your feature request related to a problem? Please describe.
We have multiple CEs, like Result, Option, Task, TaskResult, TaskResultOption.

I am relatively new to f#, but it seems to me that it is impractical to work with TaskResultOption CE, even though for single expression it's clearly nice. But if we have various levels of expressions, e.g. TaskResultOption followed by TaskResult/Result, and for them to be inside taskResultOption CE, we need to keep converting all following expressions to highest denominator (TaskResultOption).

here is a code snippet, where Mode.Parse is a Result and we need to convert it to TaskResultOption to be able to follow linearly and extract success result, same with LoadUniverseForStrategy, which is TaskResult (but not an Option):

for clarity these are signatures:

LoadLatestStrategyQueryResult: TaskResult<StrategyQueryResult option,exn>
Mode.Parse: s: string -> Result<Mode,exn>
LoadUniverseForStrategy: id: int -> TaskResult<IntUniverse,exn>
            taskResultOption {
                let! strategy = this.LoadLatestStrategyQueryResult()
                let! mode = Mode.Parse strategy.StrategyType |> TaskResult.ofResult |> TaskResult.map Some
                let! universe = this.LoadUniverseForStrategy strategy.Id |> TaskResult.map Some

                return

                    { Id = strategy.Id
                      Entry = strategy.Entry
                      Exit = strategy.Exit
                      Saved = strategy.Saved
                      Universe = universe
                      Mode = mode }
            }

Is some kind of smart CE possible, that in case of using TaskResultOption would work using let! on TaskResult and friends?
Following snippet would be ideal:

            taskResultOption {
                let! strategy = this.LoadLatestStrategyQueryResult()
                let! mode = Mode.Parse strategy.StrategyType
                let! universe = this.LoadUniverseForStrategy strategy.Id
                return

                    { Id = strategy.Id
                      Entry = strategy.Entry
                      Exit = strategy.Exit
                      Saved = strategy.Saved
                      Universe = universe
                      Mode = mode }
            }

Another option is using lower denominator like taskResult (and then conversion is simpler, but we need to resort to branching, which will become impractical once we need to pattern match more than once in a method):

            taskResult {
                let! strategy = this.LoadLatestStrategyQueryResult()

                match strategy with
                | None -> return None
                | Some s ->
                    let! mode = Mode.Parse s.StrategyType
                    let! universe = this.LoadUniverseForStrategy s.Id

                    return
                        Some
                            { Id = s.Id
                              Entry = s.Entry
                              Exit = s.Exit
                              Saved = s.Saved
                              Universe = universe
                              Mode = mode }
            }

Maybe this is just me being a novice and not seeing a straightforward way forward.

Yeah this particular CE doesn't have all the same niceness that all the others do. Any PRs would be appreciated here!

not sure if i am up to a task. Also, maybe unrelated, taskResult cannot extract from simple option with let! Any advice on this?

taskResult cannot extract from simple option with let! Any advice on this?

Yeah TaskResult knows nothing about options. You'll have to convert the Option to a Result.

yes, makes sense using Result.requireSome as an example, thanks