- Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathinfix.t
283 lines (234 loc) · 8.09 KB
/
infix.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
useTest;
plan50;
{
subinfix:<×> ($a, $b) { $a+$b }
is(5 × 3, 8, "infix Unicode operator (actual overloading)");
}
{
subinfix:<C> ($text, $owner) { return"$text copyright $owner"; };
is"romeo & juliet" C "Shakespeare", "romeo & juliet copyright Shakespeare",
'infix operator overloading for new operator';
}
{
subinfix:<©> ($text, $owner) { return"$text Copyright $owner"; };
is"romeo & juliet" © "Shakespeare", "romeo & juliet Copyright Shakespeare",
'infix operator overloading for new operator (unicode)';
}
{
subinfix:<(C)> ($text, $owner) { return"$text CopyRight $owner"; };
isEVAL(q[ "romeo & juliet" (C) "Shakespeare" ]), "romeo & juliet CopyRight Shakespeare",
'infix operator overloading for new operator (nasty)';
}
{
subinfix:«_<_ »($one, $two) { return42 } #OK not used
is3 _<_ 5, 42, "frenchquoted infix sub";
}
# unfreak perl6.vim: >>
# Overloading by setting the appropriate code variable
# https://github.com/Raku/old-issue-tracker/issues/4128
#?rakudo skip "cannot bind with this LHS"
{
my&infix:<plus>;
BEGIN {
&infix:<plus> := { $^a+$^b };
}
is3 plus 5, 8, 'overloading an operator using "my &infix:<...>" worked';
}
# Overloading by setting the appropriate code variable using symbolic
# dereferentiation
{
my&infix:<times>;
BEGIN {
&::("infix:<times>") = { $^a*$^b };
}
is3times5, 15, 'operator overloading using symbolic dereferentiation';
}
# Accessing an operator using its subroutine name
{
is&infix:<+>(2, 3), 5, "accessing a builtin operator using its subroutine name";
my&infix:<z> := { $^a+$^b };
is&infix:<z>(2, 3), 5, "accessing a userdefined operator using its subroutine name";
is~(&infix:<»+«>([1,2,3],[4,5,6])), "5 7 9", "accessing a hyperoperator using its subroutine name";
}
# Overriding infix:<;>
#?rakudo todo 'infix:<;>'
{
my proto infix:<;> ($a, $b) { $a+$b }
is$(3 ; 2), 5# XXX correct?
}
# here is one that co-erces a MyClass into a Str and a Num.
{
classOtherClass {
has$.xisrw;
}
classMyClass {
methodprefix:<~> isexport { "hi" }
methodprefix:<+> isexport { 42 }
methodinfix:<as>($self: OtherClass $to) isexport { #OK not used
my$obj=$to.new;
$obj.x=23;
return$obj;
}
}
import MyClass; # should import that sub forms of the exports
my$obj;
lives-ok { $obj= MyClass.new }, "instantiation of a prefix:<...> and infix:<as> overloading class worked";
lives-ok { ~$obj }, "our object can be stringified";
is~$obj, "hi", "our object was stringified correctly";
isEVAL('($obj as OtherClass).x'), 23, "our object was coerced correctly";
}
# https://github.com/Raku/old-issue-tracker/issues/4131
#?rakudo skip 'infix Z will never work; no lexical Z RT #124983'
{
mysubinfix:<Z> ($a, $b) {
$a**$b;
}
is (2Z1Z2), 4, "default Left-associative works.";
}
# https://github.com/Raku/old-issue-tracker/issues/4131
#?rakudo skip 'no lexical Z RT #124983'
{
mysubinfix:<Z> ($a, $b) isassoc('left') {
$a**$b;
}
is (2Z1Z2), 4, "Left-associative works.";
}
# https://github.com/Raku/old-issue-tracker/issues/4131
#?rakudo skip 'no lexical Z RT #124983'
{
mysubinfix:<Z> ($a, $b) isassoc('right') {
$a**$b;
}
is (2Z1Z2), 2, "Right-associative works.";
}
# https://github.com/Raku/old-issue-tracker/issues/4131
#?rakudo skip 'no lexical Z RT #124983'
{
mysubinfix:<Z> ($a, $b) isassoc('chain') {
$aeq$b;
}
is (1Z1Z1), Bool::True, "Chain-associative works.";
is (1Z1Z2), Bool::False, "Chain-associative works.";
}
{
subinfix:<our_non_assoc_infix> ($a, $b) isassoc('non') {
$a**$b;
}
is (2 our_non_assoc_infix 3), (2**3), "Non-associative works for just tow operands.";
is ((2 our_non_assoc_infix 2) our_non_assoc_infix 3), (2**2) **3, "Non-associative works when used with parens.";
throws-like'2 our_non_assoc_infix 3 our_non_assoc_infix 4',
X::Syntax::NonAssociative,
"Non-associative should not parsed when used chainly.";
}
{
roleA { has$.v }
multisubinfix:<==>(A $a, A $b) { $a.v ==$b.v }
lives-ok { 3==3ordie() }, 'old == still works on integers (+)';
lives-ok { 3==4anddie() }, 'old == still works on integers (-)';
ok (A.new(v =>3) == A.new(v =>3)), 'infix:<==> on A objects works (+)';
ok!(A.new(v =>2) == A.new(v =>3)), 'infix:<==> on A objects works (-)';
}
{
multisubinfix:<+=> (Int$aisrw, Int$b) { $a-=$b }
my$frew=10;
$frew+=5;
is$frew, 5, 'infix redefinition of += works';
}
{
classMMDTestType {
has$.aisrw;
methodadd(MMDTestType $b) { $.a~$b.a }
}
multisubinfix:<+>(MMDTestType $a, MMDTestType $b) { $a.add($b) };
my MMDTestType $a.=new(a=>'foo');
my MMDTestType $b.=new(a=>'bar');
is$a+$b, 'foobar', 'can overload exiting operators (here: infix:<+>)';
}
# test that multis with other arity don't interfere with existing ones
# https://github.com/Raku/old-issue-tracker/issues/993
# used to be RT #65640
{
multisubinfix:<+>() { 42 };
ok5+5==10, "New multis don't disturb old ones";
}
# https://github.com/Raku/old-issue-tracker/issues/992
{
isEVAL('sub infix:<,>($a, $b) { 42 }; 5, 5'), 42, 'infix:<,>($a, $b)';
isEVAL('sub infix:<,>(Int $x where 1, Int $y where 1) { 42 }; 1, 1'), 42,
'very specific infix:<,>';
#?rakudo todo 'RT #65638'
isEVAL('sub infix:<#>($a, $b) { 42 }; 5 # 5'), 42, 'infix:<comment char>($a, $b)';
isEVAL('multi sub infix:<+>() { 42 }; 5 + 5'), 10, 'infix:<+>()';
isEVAL('sub infix:<+>($a, $b) { 42 }; 5 + 5'), 42, 'infix:<+>($a, $b)';
}
{
multisubinfix:<foo>($a, $b) {$a+$b};
# autoviv tries to call &[foo]() with no arguments, so we define first
# alternative is below, with a candidate with an empty parameter list
my$x=0;
$x foo=6;
is$x, 6, 'foo= works for custom operators';
}
{
multisubinfix:<foo>($a, $b) {$a+$b};
multisubinfix:<foo>() { 0 };
# alternative with a candidate with an empty parameter list
my$x foo=6;
is$x, 6, 'foo= works for custom operators';
}
{
oursubinfix:<bar>($a, $b) {$a+$b};
# similar to above, but without the empty param candidate
my$x=0;
$x bar=6;
is$x, 6, 'bar= works for custom operators';
}
# https://github.com/Raku/old-issue-tracker/issues/1669
{
classRT74104 {}
multisubinfix:<+>(RT74104 $, RT74104 $) { -1 }
is2+2, 4, 'overloading an operator does not hide other candidates';
}
# https://github.com/Raku/old-issue-tracker/issues/2656
# https://github.com/Raku/old-issue-tracker/issues/2760
{
subinfix:<*+>($a, $b) { $a*$b+$b }
is2*+5, 15, 'longest operator wins (RT #111418)';
subinfix:<~eq>(Str$a, Str$b) { uc($a) equc($b) }
ok'a'~eq'A', 'longest operator wins (RT #112870)';
}
# https://github.com/Raku/old-issue-tracker/issues/2639
{
my&infix:<c> = { $^a+$^b };
is1 c 2, 3, 'assignment to code variable works.';
}
isinfix:['+'](2,3), 5, 'can call existing infix via compile-time string lookup';
isinfix:['Z~'](<a b>, <c d>), 'ac bd', 'can call autogen infix via compile-time string lookup';
# https://github.com/Raku/old-issue-tracker/issues/1183
{
BEGINmy$plus='+';
is&infix:<<$plus>>(3,4), 7, '&infix:<<$foo>> works';
is&infix:«$plus»(3,4), 7, '&infix:«$foo» works';
is&infix:[$plus](3,4), 7, '&infix:[$foo] works';
}
# https://github.com/rakudo/rakudo/issues/3895
{
my%calls;
mysubinfix:<==⨧>($a,$b) {
my$ans=so$a==$b+2;
push%calls<plain>,
"$a ==⨧ $b→$ans";
$ans
}
mysubinfix:<is-eq⨧>($a,$b) isequiv(&infix:<==>) {
my$ans=so$a==$b+2;
push%calls<is-eq⨧>,
"$a ==⨧ $b→$ans";
$ans
}
ok!(6==⨧ 4==⨧ 2), "Custom operator doesn't chain";
ok!(6 is-eq⨧ 4 is-eq⨧ 2),
"Custom operator with precedence same as == still doesn't chain";
is-deeply%calls<plain>, %calls<is-eq⨧>, "Both are called the same";
}
# vim: expandtab shiftwidth=4