- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 155
 
Open
Labels
Description
Describe the bug
When a right-biased type is wrapping an array value, it's not possible to unwrap it via pattern-matching.
To Reproduce
require "dry/monads"
include Dry::Monads[:result]
Success(1) in Success(x) # => true
x # => 1 AS EXPECTED
Success([1]) in Success(x) # => true
x # => 1 IT SHOULD BE [1]
Success([1, 2]) in Success(x) # => true
x # => 1 IT SHOULD BE [1, 2]Expected behavior
The wrapped value should be extracted as it is.
My environment
- Affects my production application: NO
 - Ruby version: v3.2
 
Proposed solution
We should stop branching depending on whether the wrapped value is an array. Instead, #deconstruct should be simply:
def deconstruct
  if Unit.equal?(@value)
    EMPTY_ARRAY
  else
    [@value]
  end
endHowever, the above would be a breaking change. Now we have:
Success([1]) in [1] # => trueThe above would change:
Success([1]) in [1] # => falseIMO, we should nonetheless change it, as the most common use-case for results is extracting the wrapped value after some operations have been performed.
I'm happy to submit a PR fixing the issue. If we consider it as a bug fix, it should go into the next minor or patch release. However, if we consider it as a new feature breaking code, we should wait until v2.0.