option
option copied to clipboard
Adding scala-style matching
This PR adds scala-style matching to options so you can do stuff like this:
result = Option(variable).match do |matcher|
matcher.case(Some[Fixnum]) { |val| val * 2 }
matcher.case(Some[String]) { |val| val.length }
end
result # => 10 when variable is 5, 6, when variable is "option"
You can also match without types:
Option(variable).match do |matcher|
matcher.case(Some) { |val| puts val }
matcher.case(None) { |val| raise StandardError }
end
Finally, the OptionMatcher also supports else:
Option(variable).match do |matcher|
matcher.case(Some[String]) { ... }
matcher.else { |val| ... }
end
ack
I'm a little swamped right now so I will probably take me a few weeks to review.
Thanks!