Skip to content

Commit acca283

Browse files
author
Tadeusz Sośnierz
committed
[advent] A word about ACCEPTS in smartmaching article
1 parent fc49a66 commit acca283

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

misc/perl6advent-2010/articles/smartmatching.pod

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,23 @@ way to match a regex, it has no special operator.
6565
While most smart matching cases return a Bool, matching against a regex
6666
returns a C<Match> object - which behaves appropriately in boolean context
6767
too.
68+
69+
You have probably started wondering: a'right, that for built-in types,
70+
how do I use it in my own classes? You need to write a special ACCEPTS
71+
method for it. Say we have our good, old class Point:
72+
73+
class Point {
74+
has $.x;
75+
has $.y;
76+
method ACCEPTS(Positional $p2) {
77+
return $.x == $p2[0] and $.y == $p2[1]
78+
}
79+
}
80+
81+
Everything clear? Let's see how it works:
82+
83+
my $a = Point.new(x => 7, y => 9);
84+
say [3, 5] ~~ $a; # Bool::False
85+
say (7, 9) ~~ $a; # Bool::True
86+
87+
# Further ideas -- why not symetric?

0 commit comments

Comments
 (0)