codex-storage / questionable

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

`without` statement can fail in generic code #11

Closed markspanbroek closed 2 years ago

markspanbroek commented 2 years ago

Context

Use the without statement on an expression that returns Result whereby you access the error inside generic code.

Failure

The error is set to nil, whereas it should have a value

Reproduce

  test "without statement works in generic code":
    proc test(_: type) =
      without a =? int.failure "error", e:
        check e.msg == "error"
        return
      fail

    test(int)

Removing the generic parameter makes the test pass.

Possible cause

Replacing infix operator =? with override=? override doesn't seem to work. It seems that the =? has already been evaluated by the compiler before it evaluates the without template.

https://github.com/status-im/questionable/blob/d7e9f0bf7fec14df13a26e699437a2fe577b26ba/questionable/withoutresult.nim#L37