option icon indicating copy to clipboard operation
option copied to clipboard

Adding scala-style matching

Open camertron opened this issue 11 years ago • 1 comments

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

camertron avatar Mar 10 '14 19:03 camertron

ack

I'm a little swamped right now so I will probably take me a few weeks to review.

Thanks!

rares avatar Mar 11 '14 20:03 rares