You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
caseTry{n / d}inValue(x)puts"n / d = #{x}"inError(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 fineres=>Value(x)putsx# => 1# Error succeeds on pattern matcherr=>Try::Error(ex)# But binds ex to nil, and ex != err.exceptionputsex.inspect=>nilputserr.exception=>#<ZeroDivisionError: divided by 0># Workarounderr.to_result=>Failure(ex)putsex.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
The text was updated successfully, but these errors were encountered:
Describe the bug
The following code should work:
But it does not - because
Try
implementsdeconstruct
fromRightBiased
, 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 onError
values never has the expected results.Current workarounds: call
#to_result
.To Reproduce
Expected behavior
Pattern matching on
Try::Error
values should bind the exception.My environment
>= 3.0
)The text was updated successfully, but these errors were encountered: