Skip to content

Instantly share code, notes, and snippets.

@rcmlz
Last active October 20, 2023 17:44
Show Gist options
  • Save rcmlz/7d0d8f7e8c61505550b416f26e7f87e5 to your computer and use it in GitHub Desktop.
Save rcmlz/7d0d8f7e8c61505550b416f26e7f87e5 to your computer and use it in GitHub Desktop.
Behavior should be specialized depending on the dynamic types of both arguments. Assume I have a Shape class that knows how derive the intersection of any Shapes. Assume I have a Rectangle class, that is extra clever and knows how to intersect Rectangles efficiently.
# Shape ∩ Shape -> Shape
# Shape ∩ Rectangle -> Shape
# Rectangle ∩ Shape -> Shape
# Rectangle ∩ Rectangle -> Rectangle
class Shape {
#| Universal Shape ∩ Shape --> Shape
multi method intersect(Shape $s --> Shape){
return Shape.new
}
}
class Rectangle is Shape {
#| Specialized Rectangle ∩ Rectangle --> Rectangle
multi method intersect(Rectangle $s --> Rectangle){
return Rectangle.new
}
}
class Client {
has @.pairs-of-shapes;
method intersect-all {
gather {
for @!pairs-of-shapes -> ($s1, $s2) {
take $s1.intersect($s2)
}
}
}
}
use Test;
my @arr of Shape = Shape.new, Rectangle.new;
my @pos = (@arr X @arr);
is-deeply Client.new(pairs-of-shapes => @pos).intersect-all,
(Shape.new, Shape.new, Shape.new, Rectangle.new);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment