/
funclit.d
536 lines (446 loc) · 16.2 KB
/
funclit.d
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import core.vararg;
extern (C) int printf(const char*, ...);
/***************************************************/
// lambda syntax check
auto una(alias dg)(int n)
{
return dg(n);
}
auto bin(alias dg)(int n, int m)
{
return dg(n, m);
}
void test1()
{
assert(una!( a => a*2 )(2) == 4);
assert(una!( ( a) => a*2 )(2) == 4);
assert(una!( (int a) => a*2 )(2) == 4);
assert(una!( ( a){ return a*2; } )(2) == 4);
assert(una!( function ( a){ return a*2; } )(2) == 4);
assert(una!( function int( a){ return a*2; } )(2) == 4);
assert(una!( function (int a){ return a*2; } )(2) == 4);
assert(una!( function int(int a){ return a*2; } )(2) == 4);
assert(una!( delegate ( a){ return a*2; } )(2) == 4);
assert(una!( delegate int( a){ return a*2; } )(2) == 4);
assert(una!( delegate (int a){ return a*2; } )(2) == 4);
assert(una!( delegate int(int a){ return a*2; } )(2) == 4);
// partial parameter specialization syntax
assert(bin!( ( a, b) => a*2+b )(2,1) == 5);
assert(bin!( (int a, b) => a*2+b )(2,1) == 5);
assert(bin!( ( a, int b) => a*2+b )(2,1) == 5);
assert(bin!( (int a, int b) => a*2+b )(2,1) == 5);
assert(bin!( ( a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( (int a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( ( a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( (int a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function ( a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function (int a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function ( a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function (int a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function int( a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function int(int a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function int( a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( function int(int a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate ( a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate (int a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate ( a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate (int a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate int( a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate int(int a, b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate int( a, int b){ return a*2+b; } )(2,1) == 5);
assert(bin!( delegate int(int a, int b){ return a*2+b; } )(2,1) == 5);
}
/***************************************************/
// on initializer
void test2()
{
// explicit typed binding ignite parameter types inference
int function(int) fn1 = a => a*2; assert(fn1(2) == 4);
int function(int) fn2 = ( a){ return a*2; }; assert(fn2(2) == 4);
int function(int) fn3 = function ( a){ return a*2; }; assert(fn3(2) == 4);
int function(int) fn4 = function int( a){ return a*2; }; assert(fn4(2) == 4);
int function(int) fn5 = function (int a){ return a*2; }; assert(fn5(2) == 4);
int function(int) fn6 = function int(int a){ return a*2; }; assert(fn6(2) == 4);
int delegate(int) dg1 = a => a*2; assert(dg1(2) == 4);
int delegate(int) dg2 = ( a){ return a*2; }; assert(dg2(2) == 4);
int delegate(int) dg3 = delegate ( a){ return a*2; }; assert(dg3(2) == 4);
int delegate(int) dg4 = delegate int( a){ return a*2; }; assert(dg4(2) == 4);
int delegate(int) dg5 = delegate (int a){ return a*2; }; assert(dg5(2) == 4);
int delegate(int) dg6 = delegate int(int a){ return a*2; }; assert(dg6(2) == 4);
// funciton/delegate mismatching always raises an error
static assert(!__traits(compiles, { int function(int) xfg3 = delegate ( a){ return a*2; }; }));
static assert(!__traits(compiles, { int function(int) xfg4 = delegate int( a){ return a*2; }; }));
static assert(!__traits(compiles, { int function(int) xfg5 = delegate (int a){ return a*2; }; }));
static assert(!__traits(compiles, { int function(int) xfg6 = delegate int(int a){ return a*2; }; }));
static assert(!__traits(compiles, { int delegate(int) xdn3 = function ( a){ return a*2; }; }));
static assert(!__traits(compiles, { int delegate(int) xdn4 = function int( a){ return a*2; }; }));
static assert(!__traits(compiles, { int delegate(int) xdn5 = function (int a){ return a*2; }; }));
static assert(!__traits(compiles, { int delegate(int) xdn6 = function int(int a){ return a*2; }; }));
// auto binding requires explicit parameter types at least
static assert(!__traits(compiles, { auto afn1 = a => a*2; }));
static assert(!__traits(compiles, { auto afn2 = ( a){ return a*2; }; }));
static assert(!__traits(compiles, { auto afn3 = function ( a){ return a*2; }; }));
static assert(!__traits(compiles, { auto afn4 = function int( a){ return a*2; }; }));
static assert(!__traits(compiles, { auto adg3 = delegate ( a){ return a*2; }; }));
static assert(!__traits(compiles, { auto adg4 = delegate int( a){ return a*2; }; }));
auto afn5 = function (int a){ return a*2; }; assert(afn5(2) == 4);
auto afn6 = function int(int a){ return a*2; }; assert(afn6(2) == 4);
auto adg5 = delegate (int a){ return a*2; }; assert(adg5(2) == 4);
auto adg6 = delegate int(int a){ return a*2; }; assert(adg6(2) == 4);
// partial specialized lambda
string delegate(int, string) dg =
(n, string s){
string r = "";
foreach (_; 0..n) r~=s;
return r;
};
assert(dg(2, "str") == "strstr");
}
/***************************************************/
// on return statement
void test3()
{
// inference matching system is same as on initializer
int delegate(int) mul(int x)
{
return a => a * x;
}
assert(mul(5)(2) == 10);
}
/***************************************************/
// on function arguments
auto foo4(int delegate(int) dg) { return dg(10); }
auto foo4(int delegate(int, int) dg) { return dg(10, 20); }
void nbar4fp(void function(int) fp) { }
void nbar4dg(void delegate(int) dg) { }
void tbar4fp(T,R)(R function(T) dg) { static assert(is(typeof(dg) == void function(int))); }
void tbar4dg(T,R)(R delegate(T) dg) { static assert(is(typeof(dg) == void delegate(int))); }
auto nbaz4(void function() fp) { return 1; }
auto nbaz4(void delegate() dg) { return 2; }
auto tbaz4(R)(R function() dg) { static assert(is(R == void)); return 1; }
auto tbaz4(R)(R delegate() dg) { static assert(is(R == void)); return 2; }
auto thoo4(T)(T lambda){ return lambda; }
void tfun4a()(int function(int) a){}
void tfun4b(T)(T function(T) a){}
void tfun4c(T)(T f){}
void test4()
{
int v;
// parameter type inference + overload resolution
assert(foo4((a) => a * 2) == 20);
assert(foo4((a,b) => a * 2 + b) == 40);
// function/delegate inference
nbar4fp((int x){ });
nbar4dg((int x){ });
tbar4fp((int x){ });
tbar4dg((int x){ });
// function/delegate inference + overload resolution
assert(nbaz4({ }) == 1);
assert(nbaz4({ v = 1; }) == 2);
assert(tbaz4({ }) == 1);
assert(tbaz4({ v = 1; }) == 2);
// template function deduction
static assert(is(typeof(thoo4({ })) : void function()));
static assert(is(typeof(thoo4({ v = 1; })) : void delegate()));
tfun4a(a => a);
static assert(!__traits(compiles, { tfun4b(a => a); }));
static assert(!__traits(compiles, { tfun4c(a => a); }));
}
void fsvarg4(int function(int)[] a...){}
void fcvarg4(int dummy, ...){}
void tsvarg4a()(int function(int)[] a...){}
void tsvarg4b(T)(T function(T)[] a...){}
void tsvarg4c(T)(T [] a...){}
void tcvarg4()(int dummy, ...){}
void test4v()
{
fsvarg4(function(int a){ return a; }); // OK
fsvarg4(a => a); // OK
fcvarg4(0, function(int a){ return a; }); // OK
static assert(!__traits(compiles, { fcvarg4(0, a => a); }));
tsvarg4a(function(int a){ return a; }); // OK
tsvarg4b(function(int a){ return a; }); // OK
tsvarg4c(function(int a){ return a; }); // OK
tsvarg4a(a => a);
static assert(!__traits(compiles, { tsvarg4b(a => a); }));
static assert(!__traits(compiles, { tsvarg4c(a => a); }));
tcvarg4(0, function(int a){ return a; }); // OK
static assert(!__traits(compiles, { tcvarg4(0, a => a); }));
}
/***************************************************/
// on CallExp::e1
void test5()
{
assert((a => a*2)(10) == 20);
assert(( a, s){ return s~s; }(10, "str") == "strstr");
assert((int a, s){ return s~s; }(10, "str") == "strstr");
assert(( a, string s){ return s~s; }(10, "str") == "strstr");
assert((int a, string s){ return s~s; }(10, "str") == "strstr");
}
/***************************************************/
// escape check to nested function symbols
void checkNestedRef(alias dg)(bool isnested)
{
static if (is(typeof(dg) == delegate))
enum isNested = true;
else static if ((is(typeof(dg) PF == F*, F) && is(F == function)))
enum isNested = false;
else
static assert(0);
assert(isnested == isNested);
dg();
}
void freeFunc(){}
void test6()
{
static void localFunc(){}
void nestedLocalFunc(){}
checkNestedRef!({ })(false);
checkNestedRef!({ freeFunc(); })(false);
checkNestedRef!({ localFunc(); })(false);
checkNestedRef!({ nestedLocalFunc(); })(true);
checkNestedRef!({ void inner(){} inner(); })(false);
checkNestedRef!({ auto f = &freeFunc; })(false);
checkNestedRef!({ auto f = &localFunc; })(false);
checkNestedRef!({ auto f = &nestedLocalFunc; })(true);
checkNestedRef!({ void inner(){} auto f = &inner; })(false);
}
/***************************************************/
// on AssignExp::e2
void test7()
{
int function(int) fp;
fp = a => a;
fp = (int a) => a;
fp = function(int a) => a;
fp = function int(int a) => a;
static assert(!__traits(compiles, { fp = delegate(int a) => a; }));
static assert(!__traits(compiles, { fp = delegate int(int a) => a; }));
int delegate(int) dg;
dg = a => a;
dg = (int a) => a;
dg = delegate(int a) => a;
dg = delegate int(int a) => a;
static assert(!__traits(compiles, { dg = function(int a) => a; }));
static assert(!__traits(compiles, { dg = function int(int a) => a; }));
}
/***************************************************/
// on StructLiteralExp::elements
void test8()
{
struct S
{
int function(int) fp;
}
auto s1 = S(a => a);
static assert(!__traits(compiles, { auto s2 = S((a, b) => a); }));
}
/***************************************************/
// on concat operation
void test9()
{
int function(int)[] a2;
a2 ~= x => x;
}
/***************************************************/
// on associative array key
void test10()
{
int[int function()] aa;
assert(!aa.remove(() => 1));
int[int function(int)] aa2;
assert(!aa2.remove(x => 1));
}
/***************************************************/
// on common type deduction
void test11()
{
auto a1 = [x => x, (int x) => x * 2];
static assert(is(typeof(a1[0]) == int function(int) pure @safe nothrow));
assert(a1[0](10) == 10);
assert(a1[1](10) == 20);
//int n = 10;
//auto a2 = [x => n, (int x) => x * 2];
//static assert(is(typeof(a2[0]) == int delegate(int) @safe nothrow));
//assert(a2[0](99) == 10);
//assert(a2[1](10) == 20);
int function(int) fp = true ? (x => x) : (x => x*2);
assert(fp(10) == 10);
int m = 10;
int delegate(int) dg = true ? (x => x) : (x => m*2);
assert(dg(10) == 10);
}
/***************************************************/
// 3235
void test3235()
{
// from TDPL
auto f = (int i) {};
static if (is(typeof(f) _ == F*, F) && is(F == function))
{} else static assert(0);
}
/***************************************************/
// 6714
void foo6714x(int function (int, int) a){}
void bar6714x(int delegate (int, int) a){}
int bar6714y(double delegate(int, int) a){ return 1; }
int bar6714y( int delegate(int, int) a){ return 2; }
void test6714()
{
foo6714x((a, b) { return a + b; });
bar6714x((a, b) { return a + b; });
assert(bar6714y((a, b){ return 1.0; }) == 1);
static assert(!__traits(compiles, {
bar6714y((a, b){ return 1.0f; });
}));
assert(bar6714y((a, b){ return a; }) == 2);
}
/***************************************************/
// 7193
void test7193()
{
static assert(!__traits(compiles, {
delete a => a;
}));
}
/***************************************************/
// 7207 : on CastExp
void test7202()
{
auto dg = cast(int function(int))(a => a);
assert(dg(10) == 10);
}
/***************************************************/
// 7288
void test7288()
{
// 7288 -> OK
auto foo()
{
int x;
return () => { return x; };
}
pragma(msg, typeof(&foo));
alias int delegate() nothrow @safe delegate() nothrow @safe delegate() Dg;
pragma(msg, Dg);
static assert(is(typeof(&foo) == Dg)); // should pass
}
/***************************************************/
// 7499
void test7499()
{
int function(int)[] a1 = [ x => x ]; // 7499
int function(int)[][] a2 = [[x => x]]; // +a
assert(a1[0] (10) == 10);
assert(a2[0][0](10) == 10);
}
/***************************************************/
// 7500
void test7500()
{
alias immutable bool function(int[]) Foo;
Foo f = a => true;
}
/***************************************************/
// 7525
void test7525()
{
{
char[] delegate() a = { return null; };
int delegate() b = { return 1U; };
uint delegate() c = { return 1; };
float delegate() d = { return 1.0; };
double delegate() e = { return 1.0f; };
}
{
char[] delegate(int) a = (x){ return null; };
int delegate(int) b = (x){ return 1U; };
uint delegate(int) c = (x){ return 1; };
float delegate(int) d = (x){ return 1.0; };
double delegate(int) e = (x){ return 1.0f; };
}
}
/***************************************************/
// 7582
void test7582()
{
void delegate(int) foo;
void delegate(int) foo2;
foo = (a) {
foo2 = (b) { };
};
}
/***************************************************/
// 7649
void test7649()
{
void foo(int function(int) fp = x => 1)
{
assert(fp(1) == 1);
}
foo();
}
/***************************************************/
// 7650
void test7650()
{
int[int function(int)] aa1 = [x=>x:1, x=>x*2:2];
foreach (k, v; aa1) {
if (v == 1) assert(k(10) == 10);
if (v == 2) assert(k(10) == 20);
}
int function(int)[int] aa2 = [1:x=>x, 2:x=>x*2];
assert(aa2[1](10) == 10);
assert(aa2[2](10) == 20);
int n = 10;
int[int delegate(int)] aa3 = [x=>n+x:1, x=>n+x*2:2];
foreach (k, v; aa3) {
if (v == 1) assert(k(10) == 20);
if (v == 2) assert(k(10) == 30);
}
int delegate(int)[int] aa4 = [1:x=>n+x, 2:x=>n+x*2];
assert(aa4[1](10) == 20);
assert(aa4[2](10) == 30);
}
/***************************************************/
// 7705
void test7705()
{
void foo1(void delegate(ref int ) dg){ int x=10; dg(x); }
foo1((ref x){ pragma(msg, typeof(x)); assert(x == 10); });
static assert(!__traits(compiles, foo1((x){}) ));
void foo2(void delegate(int, ...) dg){ dg(20, 3.14); }
foo2((x,...){ pragma(msg, typeof(x)); assert(x == 20); });
void foo3(void delegate(int[]...) dg){ dg(1, 2, 3); }
foo3((x ...){ pragma(msg, typeof(x)); assert(x == [1,2,3]); });
}
/***************************************************/
int main()
{
test1();
test2();
test3();
test4();
test4v();
test5();
test6();
test7();
test8();
test9();
test10();
test11();
test3235();
test6714();
test7193();
test7202();
test7288();
test7499();
test7500();
test7525();
test7582();
test7649();
test7650();
test7705();
printf("Success\n");
return 0;
}