codex-storage / questionable

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

Extend withoutresult.nim to support arbitrary Result[T,E] types #26

Closed Bulat-Ziganshin closed 1 year ago

Bulat-Ziganshin commented 1 year ago

Can it be done?

markspanbroek commented 1 year ago

I don't know of a way to do this well, unfortunately. Here's an example to illustrate why this is problematic:

proc foo: Result[int, string]
proc bar: Result[int, ref CatchableError]

without a =? foo() and b =? bar(), error:
  # what is the type of the `error` variable here?
Menduist commented 1 year ago

You could just block and to allow only the same error type, and still allow without for basic usage?

markspanbroek commented 1 year ago

You could just block and to allow only the same error type, and still allow without for basic usage?

I think that that would be hard to achieve. The without macro currently doesn't know anything about the condition that it's evaluating. It doesn't see the and. It only knows that the condition should evaluate to a boolean. The bindings =? are implemented as boolean expressions with the side effect of assigning the value of a result to a variable. The and is just the simple operator on booleans, and doesn't know that it's operating on bindings.

dryajov commented 1 year ago

There is also an extensive discussion here - https://github.com/status-im/nim-codex/discussions/41, which should explain the reasoning behind the Result[T, CathableError] in questionable.