Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try::Error has an incorrect deconstruct implementation #176

Open
alexkalderimis opened this issue Jul 22, 2024 · 0 comments · May be fixed by #177
Open

Try::Error has an incorrect deconstruct implementation #176

alexkalderimis opened this issue Jul 22, 2024 · 0 comments · May be fixed by #177

Comments

@alexkalderimis
Copy link

Describe the bug

The following code should work:

case Try { n / d }
in Value(x)
  puts "n / d = #{x}"
in Error(ZeroDivisionError => ex)
  puts "divisor must not be zero"
else [x]
  puts "Got exception: #{x}"
end

But it does not - because Try implements deconstruct from RightBiased, which expects the value to be stored at the @value instance variable.

Try::Error does not have a @value variable - it uses @exception instead. Thus pattern matching on Error values never has the expected results.

Current workarounds: call #to_result.

To Reproduce

res = Try { 3 / 2 }
err = Try { 1 / 0 }

# Value works fine
res => Value(x)
puts x # => 1

# Error succeeds on pattern match
err => Try::Error(ex)

# But binds ex to nil, and ex != err.exception
puts ex.inspect => nil
puts err.exception => #<ZeroDivisionError: divided by 0>

# Workaround
err.to_result => Failure(ex)
puts ex.inspect => #<ZeroDivisionError: divided by 0>

Expected behavior

Pattern matching on Try::Error values should bind the exception.

My environment

  • Affects my production application: YES - workarounds are available
  • Ruby version: ALL (strictly speaking >= 3.0)
  • OS: ALL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant