-
Notifications
You must be signed in to change notification settings - Fork 134
/
multi.t
52 lines (39 loc) 路 1.33 KB
/
multi.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use v6;
use Test;
plan 8;
# L<S12/"Multisubs and Multimethods">
# L<S12/"Multi dispatch">
class Foo {
multi method bar() {
return "Foo.bar() called with no args";
}
multi method bar(Str $str) {
return "Foo.bar() called with Str : $str";
}
multi method bar(Int $int) {
return "Foo.bar() called with Int : $int";
}
multi method bar(Num $num) {
return "Foo.bar() called with Num : $num";
}
}
my $foo = Foo.new();
is($foo.bar(), 'Foo.bar() called with no args', '... multi-method dispatched on no args');
is($foo.bar("Hello"), 'Foo.bar() called with Str : Hello', '... multi-method dispatched on Str');
is($foo.bar(5), 'Foo.bar() called with Int : 5', '... multi-method dispatched on Int');
my $num = '4';
is($foo.bar(+$num), 'Foo.bar() called with Num : 4', '... multi-method dispatched on Num');
role R1 {
method foo($x) { 1 }
}
role R2 {
method foo($x, $y) { 2 }
}
eval_dies_ok 'class X does R1 does R2 { }', 'sanity: get composition conflict error';
class C does R1 does R2 {
proto method foo() { "proto" }
}
my $obj = C.new;
is($obj.foo(), 'proto', 'proto caused methods from roles to be composed without conflict');
is($obj.foo('a'), 1, 'method composed into multi from role called');
is($obj.foo('a','b'), 2, 'method composed into multi from role called');