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

Pattern matching Success containing an array returns a surprising result #158

Closed
ygt-mikekchar opened this issue Oct 26, 2021 · 1 comment

Comments

@ygt-mikekchar
Copy link

Describe the bug

When you pattern match a Result that happens to contain an array in a case statement, the destructured contents of the Result contains the contents of the array, not the array.

We discovered this because we have a function that decodes JSON and returns the contents in a Result (Success if it parsed correctly, Failure if it didn't). We don't know ahead of time whether or not the Result will contain an array or not. It's not really a problem to use an if statement in this situation (result.value! contains the correct value), but it's very surprising that you have to.

To Reproduce

result = Success([42])

case result
in Success(value)
  value
in Failure(err)
  err
end

Returns 42

If instead you set: result = Success([1, 2, 3, 4])

The above case statement will generate a NoMatchingPatternError.

Expected behavior

The case statement should return [42] (or in the second instance [1, 2, 3, 4])

My environment

  • Affects my production application: YES/NO
  • Ruby version: 3.0.1
  • OS: N/A
@flash-gordon
Copy link
Member

Yeah, that was covered in docs https://dry-rb.org/gems/dry-monads/1.3/pattern-matching/#matching-array-values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants