shiika
shiika copied to clipboard
Pattern match
Something like this
class Shape; end
class Circle : Shape; def initialize(@x: Int, @y: Int, @r: Int); end; end
class Rect : Shape; def initialize(@x: Int, @y: Int, @x2: Int, @y2: Int); end; end
...
shapes.each do |shape|
case shape
when Circle(x, y, r)
...
when Rect(x, y, x2, y2)
...
end
end
Another example
case value
when 1
...
when 2, 3
...
else
...
end
First of all, we need pattern matching against enums.
enum Maybe<V>
case Some(value: V)
case None
def is_some -> bool
match self
when Some(value) then true
else false
end
end
end
Closing this because basic cases are already implemented.