shiika icon indicating copy to clipboard operation
shiika copied to clipboard

Pattern match

Open yhara opened this issue 4 years ago • 2 comments

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

yhara avatar Jan 09 '21 08:01 yhara

Another example

case value
when 1
  ...
when 2, 3
  ...
else
  ...
end

yhara avatar Jan 09 '21 08:01 yhara

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

yhara avatar Jul 23 '21 14:07 yhara

Closing this because basic cases are already implemented.

yhara avatar Dec 21 '22 14:12 yhara