codex-storage / questionable

Elegant optional types for Nim
Other
116 stars 5 forks source link

Allow chaining of `.catch.errorOption` for procs that return `void` #33

Closed emizzle closed 1 year ago

emizzle commented 1 year ago

I could possibly be doing something wrong, but I have run into situations where I'd like to chain .catch.errorOption so that an error can be caught and turned into a Result[void, CatchableError], but this isn't allowed because the called proc returns a void. For example, this doesn't compile:

import pkg/questionable

proc returnsVoid =
  raise newException(ValueError, "some error")

if err =? returnsVoid().catch.errorOption:
    echo "failed to do something, error: ", err.msg
arnetheduck commented 1 year ago

There's a bug in Results for catch fixed here: https://github.com/status-im/nim-stew/pull/201

with that fix, you can do the following with plain results:

import stew/results

proc returnsVoid =
  raise newException(ValueError, "some error")

catch(returnsVoid()).isOkOr:
  echo "error: ", error.msg

Not sure if the chaining syntax can be made to work..

markspanbroek commented 1 year ago

I tried @emizzle's example with the latest stew and latest results, and it still doesn't compile. But if we get rid of the chaining syntax as @arnetheduck suggested, then it does work:

import pkg/questionable

proc returnsVoid =
  raise newException(ValueError, "some error")

if err =? catch(returnsVoid()).errorOption:
    echo "failed to do something, error: ", err.msg

I think that this is simply one of the limitations of the method call syntax.