4 int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe
5 int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe
9 int delegate() pure nothrow @nogc @safe function() pure nothrow @safe
19 extern (C) int printf(const char*, ...);
21 /***************************************************/
22 // lambda syntax check
24 auto una(alias dg)(int n)
28 auto bin(alias dg)(int n, int m)
34 assert(una!( a => a*2 )(2) == 4);
35 assert(una!( ( a) => a*2 )(2) == 4);
36 assert(una!( (int a) => a*2 )(2) == 4);
37 assert(una!( ( a){ return a*2; } )(2) == 4);
38 assert(una!( function ( a){ return a*2; } )(2) == 4);
39 assert(una!( function int( a){ return a*2; } )(2) == 4);
40 assert(una!( function (int a){ return a*2; } )(2) == 4);
41 assert(una!( function int(int a){ return a*2; } )(2) == 4);
42 assert(una!( delegate ( a){ return a*2; } )(2) == 4);
43 assert(una!( delegate int( a){ return a*2; } )(2) == 4);
44 assert(una!( delegate (int a){ return a*2; } )(2) == 4);
45 assert(una!( delegate int(int a){ return a*2; } )(2) == 4);
47 // partial parameter specialization syntax
48 assert(bin!( ( a, b) => a*2+b )(2,1) == 5);
49 assert(bin!( (int a, b) => a*2+b )(2,1) == 5);
50 assert(bin!( ( a, int b) => a*2+b )(2,1) == 5);
51 assert(bin!( (int a, int b) => a*2+b )(2,1) == 5);
52 assert(bin!( ( a, b){ return a*2+b; } )(2,1) == 5);
53 assert(bin!( (int a, b){ return a*2+b; } )(2,1) == 5);
54 assert(bin!( ( a, int b){ return a*2+b; } )(2,1) == 5);
55 assert(bin!( (int a, int b){ return a*2+b; } )(2,1) == 5);
56 assert(bin!( function ( a, b){ return a*2+b; } )(2,1) == 5);
57 assert(bin!( function (int a, b){ return a*2+b; } )(2,1) == 5);
58 assert(bin!( function ( a, int b){ return a*2+b; } )(2,1) == 5);
59 assert(bin!( function (int a, int b){ return a*2+b; } )(2,1) == 5);
60 assert(bin!( function int( a, b){ return a*2+b; } )(2,1) == 5);
61 assert(bin!( function int(int a, b){ return a*2+b; } )(2,1) == 5);
62 assert(bin!( function int( a, int b){ return a*2+b; } )(2,1) == 5);
63 assert(bin!( function int(int a, int b){ return a*2+b; } )(2,1) == 5);
64 assert(bin!( delegate ( a, b){ return a*2+b; } )(2,1) == 5);
65 assert(bin!( delegate (int a, b){ return a*2+b; } )(2,1) == 5);
66 assert(bin!( delegate ( a, int b){ return a*2+b; } )(2,1) == 5);
67 assert(bin!( delegate (int a, int b){ return a*2+b; } )(2,1) == 5);
68 assert(bin!( delegate int( a, b){ return a*2+b; } )(2,1) == 5);
69 assert(bin!( delegate int(int a, b){ return a*2+b; } )(2,1) == 5);
70 assert(bin!( delegate int( a, int b){ return a*2+b; } )(2,1) == 5);
71 assert(bin!( delegate int(int a, int b){ return a*2+b; } )(2,1) == 5);
74 /***************************************************/
79 // explicit typed binding ignite parameter types inference
80 int function(int) fn1 = a => a*2; assert(fn1(2) == 4);
81 int function(int) fn2 = ( a){ return a*2; }; assert(fn2(2) == 4);
82 int function(int) fn3 = function ( a){ return a*2; }; assert(fn3(2) == 4);
83 int function(int) fn4 = function int( a){ return a*2; }; assert(fn4(2) == 4);
84 int function(int) fn5 = function (int a){ return a*2; }; assert(fn5(2) == 4);
85 int function(int) fn6 = function int(int a){ return a*2; }; assert(fn6(2) == 4);
86 int delegate(int) dg1 = a => a*2; assert(dg1(2) == 4);
87 int delegate(int) dg2 = ( a){ return a*2; }; assert(dg2(2) == 4);
88 int delegate(int) dg3 = delegate ( a){ return a*2; }; assert(dg3(2) == 4);
89 int delegate(int) dg4 = delegate int( a){ return a*2; }; assert(dg4(2) == 4);
90 int delegate(int) dg5 = delegate (int a){ return a*2; }; assert(dg5(2) == 4);
91 int delegate(int) dg6 = delegate int(int a){ return a*2; }; assert(dg6(2) == 4);
93 // function/delegate mismatching always raises an error
94 static assert(!__traits(compiles, { int function(int) xfg3 = delegate ( a){ return a*2; }; }));
95 static assert(!__traits(compiles, { int function(int) xfg4 = delegate int( a){ return a*2; }; }));
96 static assert(!__traits(compiles, { int function(int) xfg5 = delegate (int a){ return a*2; }; }));
97 static assert(!__traits(compiles, { int function(int) xfg6 = delegate int(int a){ return a*2; }; }));
98 static assert(!__traits(compiles, { int delegate(int) xdn3 = function ( a){ return a*2; }; }));
99 static assert(!__traits(compiles, { int delegate(int) xdn4 = function int( a){ return a*2; }; }));
100 static assert(!__traits(compiles, { int delegate(int) xdn5 = function (int a){ return a*2; }; }));
101 static assert(!__traits(compiles, { int delegate(int) xdn6 = function int(int a){ return a*2; }; }));
103 // auto binding requires explicit parameter types at least
104 static assert(!__traits(compiles, { auto afn1 = a => a*2; }));
105 static assert(!__traits(compiles, { auto afn2 = ( a){ return a*2; }; }));
106 static assert(!__traits(compiles, { auto afn3 = function ( a){ return a*2; }; }));
107 static assert(!__traits(compiles, { auto afn4 = function int( a){ return a*2; }; }));
108 static assert(!__traits(compiles, { auto adg3 = delegate ( a){ return a*2; }; }));
109 static assert(!__traits(compiles, { auto adg4 = delegate int( a){ return a*2; }; }));
110 auto afn5 = function (int a){ return a*2; }; assert(afn5(2) == 4);
111 auto afn6 = function int(int a){ return a*2; }; assert(afn6(2) == 4);
112 auto adg5 = delegate (int a){ return a*2; }; assert(adg5(2) == 4);
113 auto adg6 = delegate int(int a){ return a*2; }; assert(adg6(2) == 4);
115 // partial specialized lambda
116 string delegate(int, string) dg =
119 foreach (_; 0..n) r~=s;
122 assert(dg(2, "str") == "strstr");
125 /***************************************************/
126 // on return statement
130 // inference matching system is same as on initializer
131 int delegate(int) mul(int x)
135 assert(mul(5)(2) == 10);
138 /***************************************************/
139 // on function arguments
141 auto foo4(int delegate(int) dg) { return dg(10); }
142 auto foo4(int delegate(int, int) dg) { return dg(10, 20); }
144 void nbar4fp(void function(int) fp) { }
145 void nbar4dg(void delegate(int) dg) { }
146 void tbar4fp(T,R)(R function(T) dg) { static assert(is(typeof(dg) == void function(int))); }
147 void tbar4dg(T,R)(R delegate(T) dg) { static assert(is(typeof(dg) == void delegate(int))); }
149 auto nbaz4(void function() fp) { return 1; }
150 auto nbaz4(void delegate() dg) { return 2; }
151 auto tbaz4(R)(R function() dg) { static assert(is(R == void)); return 1; }
152 auto tbaz4(R)(R delegate() dg) { static assert(is(R == void)); return 2; }
154 auto thoo4(T)(T lambda){ return lambda; }
156 void tfun4a()(int function(int) a){}
157 void tfun4b(T)(T function(T) a){}
158 void tfun4c(T)(T f){}
163 static void sfoo() {}
166 // parameter type inference + overload resolution
167 assert(foo4((a) => a * 2) == 20);
168 assert(foo4((a,b) => a * 2 + b) == 40);
170 // function/delegate inference
176 // function/delegate inference + overload resolution
177 assert(nbaz4({ }) == 1);
178 assert(nbaz4({ v = 1; }) == 2);
179 assert(nbaz4({ sfoo(); }) == 1); // https://issues.dlang.org/show_bug.cgi?id=8836
180 assert(nbaz4({ nfoo(); }) == 2);
182 assert(tbaz4({ }) == 1);
183 assert(tbaz4({ v = 1; }) == 2);
184 assert(tbaz4({ sfoo(); }) == 1);
185 assert(tbaz4({ nfoo(); }) == 2);
187 // template function deduction
188 static assert(is(typeof(thoo4({ })) : void function()));
189 static assert(is(typeof(thoo4({ v = 1; })) : void delegate()));
192 static assert(!__traits(compiles, { tfun4b(a => a); }));
193 static assert(!__traits(compiles, { tfun4c(a => a); }));
196 void fsvarg4(int function(int)[] a...){}
197 void fcvarg4(int dummy, ...){}
199 void tsvarg4a()(int function(int)[] a...){}
200 void tsvarg4b(T)(T function(T)[] a...){}
201 void tsvarg4c(T)(T [] a...){}
202 void tcvarg4()(int dummy, ...){}
206 fsvarg4(function(int a){ return a; }); // OK
207 fsvarg4(a => a); // OK
209 fcvarg4(0, function(int a){ return a; }); // OK
210 static assert(!__traits(compiles, { fcvarg4(0, a => a); }));
212 tsvarg4a(function(int a){ return a; }); // OK
213 tsvarg4b(function(int a){ return a; }); // OK
214 tsvarg4c(function(int a){ return a; }); // OK
216 static assert(!__traits(compiles, { tsvarg4b(a => a); }));
217 static assert(!__traits(compiles, { tsvarg4c(a => a); }));
219 tcvarg4(0, function(int a){ return a; }); // OK
220 static assert(!__traits(compiles, { tcvarg4(0, a => a); }));
223 // A lambda in function default argument should be deduced to delegate, by the
224 // preparation inferType call in TypeFunction.semantic.
225 void test4_findRoot(scope bool delegate(real lo, real hi) tolerance = (real a, real b) => false)
228 /***************************************************/
233 assert((a => a*2)(10) == 20);
234 assert(( a, s){ return s~s; }(10, "str") == "strstr");
235 assert((int a, s){ return s~s; }(10, "str") == "strstr");
236 assert(( a, string s){ return s~s; }(10, "str") == "strstr");
237 assert((int a, string s){ return s~s; }(10, "str") == "strstr");
240 /***************************************************/
241 // escape check to nested function symbols
243 void checkNestedRef(alias dg)(bool isnested)
245 static if (is(typeof(dg) == delegate))
246 enum isNested = true;
247 else static if ((is(typeof(dg) PF == F*, F) && is(F == function)))
248 enum isNested = false;
252 assert(isnested == isNested);
260 static void localFunc(){}
261 void nestedLocalFunc(){}
263 checkNestedRef!({ })(false);
265 checkNestedRef!({ freeFunc(); })(false);
266 checkNestedRef!({ localFunc(); })(false);
267 checkNestedRef!({ nestedLocalFunc(); })(true);
268 checkNestedRef!({ void inner(){} inner(); })(false);
270 checkNestedRef!({ auto f = &freeFunc; })(false);
271 checkNestedRef!({ auto f = &localFunc; })(false);
272 checkNestedRef!({ auto f = &nestedLocalFunc; })(true);
273 checkNestedRef!({ void inner(){} auto f = &inner; })(false);
276 /***************************************************/
281 int function(int) fp;
284 fp = function(int a) => a;
285 fp = function int(int a) => a;
286 static assert(!__traits(compiles, { fp = delegate(int a) => a; }));
287 static assert(!__traits(compiles, { fp = delegate int(int a) => a; }));
289 int delegate(int) dg;
292 dg = delegate(int a) => a;
293 dg = delegate int(int a) => a;
294 static assert(!__traits(compiles, { dg = function(int a) => a; }));
295 static assert(!__traits(compiles, { dg = function int(int a) => a; }));
298 /***************************************************/
299 // on StructLiteralExp::elements
305 int function(int) fp;
308 static assert(!__traits(compiles, { auto s2 = S((a, b) => a); }));
311 /***************************************************/
312 // on concat operation
316 int function(int)[] a2;
320 /***************************************************/
321 // on associative array key
325 int[int function()] aa;
326 assert(!aa.remove(() => 1));
328 int[int function(int)] aa2;
329 assert(!aa2.remove(x => 1));
332 /***************************************************/
333 // on common type deduction
337 auto a1 = [x => x, (int x) => x * 2];
338 static assert(is(typeof(a1[0]) == int function(int) pure @safe nothrow @nogc));
339 assert(a1[0](10) == 10);
340 assert(a1[1](10) == 20);
343 //auto a2 = [x => n, (int x) => x * 2];
344 //static assert(is(typeof(a2[0]) == int delegate(int) @safe nothrow));
345 //assert(a2[0](99) == 10);
346 //assert(a2[1](10) == 20);
348 int function(int) fp = true ? (x => x) : (x => x*2);
349 assert(fp(10) == 10);
352 int delegate(int) dg = true ? (x => x) : (x => m*2);
353 assert(dg(10) == 10);
356 /***************************************************/
357 // https://issues.dlang.org/show_bug.cgi?id=3235
363 static if (is(typeof(f) _ == F*, F) && is(F == function))
364 {} else static assert(0);
367 /***************************************************/
368 // https://issues.dlang.org/show_bug.cgi?id=6714
370 void foo6714x(int function (int, int) a){}
371 void bar6714x(int delegate (int, int) a){}
373 int bar6714y(double delegate(int, int) a){ return 1; }
374 int bar6714y( int delegate(int, int) a){ return 2; }
378 foo6714x((a, b) { return a + b; });
379 bar6714x((a, b) { return a + b; });
381 assert(bar6714y((a, b){ return 1.0; }) == 1);
382 assert(bar6714y((a, b){ return 1.0f; }) == 1);
383 assert(bar6714y((a, b){ return a; }) == 2);
386 /***************************************************/
387 // https://issues.dlang.org/show_bug.cgi?id=7193
391 static assert(!__traits(compiles, {
396 /***************************************************/
397 // https://issues.dlang.org/show_bug.cgi?id=7207
402 auto dg = cast(int function(int))(a => a);
403 assert(dg(10) == 10);
406 /***************************************************/
407 // https://issues.dlang.org/show_bug.cgi?id=7288
415 return () { return () => x; };
417 pragma(msg, typeof(&foo));
418 alias int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe Dg;
420 static assert(is(typeof(&foo) == Dg)); // should pass
423 /***************************************************/
424 // https://issues.dlang.org/show_bug.cgi?id=7499
428 int function(int)[] a1 = [ x => x ]; // 7499
429 int function(int)[][] a2 = [[x => x]]; // +a
430 assert(a1[0] (10) == 10);
431 assert(a2[0][0](10) == 10);
434 /***************************************************/
435 // https://issues.dlang.org/show_bug.cgi?id=7500
439 alias immutable bool function(int[]) Foo;
443 /***************************************************/
444 // https://issues.dlang.org/show_bug.cgi?id=7525
449 char[] delegate() a = { return null; };
450 int delegate() b = { return 1U; };
451 uint delegate() c = { return 1; };
452 float delegate() d = { return 1.0; };
453 double delegate() e = { return 1.0f; };
457 char[] delegate(int) a = (x){ return null; };
458 int delegate(int) b = (x){ return 1U; };
459 uint delegate(int) c = (x){ return 1; };
460 float delegate(int) d = (x){ return 1.0; };
461 double delegate(int) e = (x){ return 1.0f; };
465 /***************************************************/
466 // https://issues.dlang.org/show_bug.cgi?id=7582
470 void delegate(int) foo;
471 void delegate(int) foo2;
477 /***************************************************/
478 // https://issues.dlang.org/show_bug.cgi?id=7649
482 void foo(int function(int) fp = x => 1)
489 /***************************************************/
490 // https://issues.dlang.org/show_bug.cgi?id=7650
494 int[int function(int)] aa1 = [x=>x:1, x=>x*2:2];
495 foreach (k, v; aa1) {
496 if (v == 1) assert(k(10) == 10);
497 if (v == 2) assert(k(10) == 20);
500 int function(int)[int] aa2 = [1:x=>x, 2:x=>x*2];
501 assert(aa2[1](10) == 10);
502 assert(aa2[2](10) == 20);
505 int[int delegate(int)] aa3 = [x=>n+x:1, x=>n+x*2:2];
506 foreach (k, v; aa3) {
507 if (v == 1) assert(k(10) == 20);
508 if (v == 2) assert(k(10) == 30);
511 int delegate(int)[int] aa4 = [1:x=>n+x, 2:x=>n+x*2];
512 assert(aa4[1](10) == 20);
513 assert(aa4[2](10) == 30);
516 /***************************************************/
517 // https://issues.dlang.org/show_bug.cgi?id=7705
521 void foo1(void delegate(ref int ) dg){ int x=10; dg(x); }
522 foo1((ref x){ pragma(msg, typeof(x)); assert(x == 10); });
523 static assert(!__traits(compiles, foo1((x){}) ));
525 void foo2(void delegate(int, ...) dg){ dg(20, 3.14); }
526 foo2((x,...){ pragma(msg, typeof(x)); assert(x == 20); });
528 void foo3(void delegate(int[]...) dg){ dg(1, 2, 3); }
529 foo3((x ...){ pragma(msg, typeof(x)); assert(x == [1,2,3]); });
532 /***************************************************/
533 // https://issues.dlang.org/show_bug.cgi?id=7713
535 void foo7713(T)(T delegate(in Object) dlg)
539 foo7713( (in obj) { return 15; } ); // line 6
542 /***************************************************/
543 // https://issues.dlang.org/show_bug.cgi?id=7743
555 return () nothrow => x;
559 pragma(msg, typeof(&foo7743a));
560 static assert(is(typeof(&foo7743a) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
561 assert(foo7743a()() == 10);
563 static assert(is(typeof(&foo7743b) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
564 assert(foo7743b()() == 10);
567 /***************************************************/
568 // https://issues.dlang.org/show_bug.cgi?id=7761
570 enum dg7761 = (int a) pure => 2 * a;
574 static assert(is(typeof(dg7761) == int function(int) pure @safe nothrow @nogc));
575 assert(dg7761(10) == 20);
578 /***************************************************/
579 // https://issues.dlang.org/show_bug.cgi?id=7941
583 static assert(!__traits(compiles, { enum int c = function(){}; }));
586 /***************************************************/
587 // https://issues.dlang.org/show_bug.cgi?id=8005
591 auto n = (a, int n = 2){ return n; }(1);
595 /***************************************************/
600 T delegate(T) zero(T)(T delegate(T) f)
605 T delegate(T) delegate(T delegate(T)) succ(T)(T delegate(T) delegate(T delegate(T)) n)
607 return f => x => f(n(f)(x));
610 uint delegate(uint) delegate(uint delegate(uint)) n = &zero!uint;
613 assert(n(x => x + 1)(0) == i);
618 /***************************************************/
619 // https://issues.dlang.org/show_bug.cgi?id=8226
621 immutable f8226 = (int x) => x * 2;
625 assert(f8226(10) == 20);
628 /***************************************************/
629 // https://issues.dlang.org/show_bug.cgi?id=8241
631 auto exec8241a(alias a = function(x) => x, T...)(T as)
636 auto exec8241b(alias a = (x) => x, T...)(T as)
647 /***************************************************/
648 // https://issues.dlang.org/show_bug.cgi?id=8242
650 template exec8242(alias a, T...)
658 mixin exec8242!(x => x, int);
659 mixin exec8242!((string x) => x, string);
667 /***************************************************/
668 // https://issues.dlang.org/show_bug.cgi?id=8315
676 void foo8315(alias pred)()
677 if (is(typeof(pred(1)) == bool))
680 /***************************************************/
681 // https://issues.dlang.org/show_bug.cgi?id=8397
685 void function(int) f;
686 static assert(!is(typeof({
687 f = function(string x) {};
691 /***************************************************/
692 // https://issues.dlang.org/show_bug.cgi?id=8496
696 alias extern (C) void function() Func;
703 /***************************************************/
704 // https://issues.dlang.org/show_bug.cgi?id=8575
706 template tfunc8575(func...)
708 auto tfunc8575(U)(U u) { return func[0](u); }
712 return tfunc8575!(a => a)(t);
714 void foo8575a() { assert(bar8575(uint.init + 1) == +1); }
715 void foo8575b() { assert(bar8575( int.init - 1) == -1); }
723 /***************************************************/
724 // https://issues.dlang.org/show_bug.cgi?id=9153
726 void writeln9153(string s){}
731 (string x) { writeln9153(x); },
732 (string x) { x ~= 'a'; },
735 (string x) { x ~= 'a'; },
736 (string x) { writeln9153(x); },
740 /***************************************************/
741 // https://issues.dlang.org/show_bug.cgi?id=9393
743 template ifThrown9393a(E)
745 void ifThrown9393a(T)(scope T delegate(E) errHandler)
749 void ifThrown9393b(E, T)(scope T delegate(E) errHandler)
753 void foo9393(T)(void delegate(T) dg){ dg(T.init); }
754 void foo9393()(void delegate(int) dg){ foo9393!int(dg); }
758 ifThrown9393a!Exception(e => 10);
759 ifThrown9393b!Exception(e => 10);
761 foo9393((x){ assert(x == int.init); });
764 /***************************************************/
765 // https://issues.dlang.org/show_bug.cgi?id=9415
770 typeof((int a){return z;}) dg;
771 dg = (int a){return z;};
774 /***************************************************/
775 // https://issues.dlang.org/show_bug.cgi?id=9628
777 template TypeTuple9628(TL...) { alias TypeTuple9628 = TL; }
778 void map9628(alias func)() { func(0); }
782 auto items = [[10, 20], [30]];
790 map9628!(( i){ res ~= sub.length; });
791 map9628!((size_t i){ res ~= sub.length; });
794 assert(res == [2,2,1,1, 2,2,1,1]);
797 foreach (_; TypeTuple9628!(0, 1))
801 map9628!(( i){ res ~= sub.length; });
802 map9628!((size_t i){ res ~= sub.length; });
805 assert(res == [2,2,1,1, 2,2,1,1]);
808 /***************************************************/
809 // https://issues.dlang.org/show_bug.cgi?id=9928
813 void* smth = (int x) { return x; };
816 /***************************************************/
817 // https://issues.dlang.org/show_bug.cgi?id=10133
819 ptrdiff_t countUntil10133(alias pred, R)(R haystack)
825 foreach (T elem; haystack)
827 if (pred(elem)) return i;
834 bool func10133(string s)() if (countUntil10133!(x => x == 'x')(s) == 1)
839 bool func10133a(string s)() if (countUntil10133!(x => s == "x")(s) != -1)
843 bool func10133b(string s)() if (countUntil10133!(x => s == "x")(s) != -1)
853 static assert(!is(typeof(func10133a!("ax")())));
854 static assert(!is(typeof(func10133b!("ax")())));
858 /***************************************************/
859 // https://issues.dlang.org/show_bug.cgi?id=10219
866 void test_dg(I delegate(C) dg)
869 void* cptr = cast(void*) c;
870 void* iptr = cast(void*) cast(I) c;
871 void* xptr = cast(void*) dg(c);
872 assert(cptr != iptr);
873 assert(cptr != xptr); // should pass
874 assert(iptr == xptr); // should pass
877 C delegate(C c) dg = delegate C(C c) { return c; };
878 static assert(!__traits(compiles, { test_dg(dg); }));
879 static assert(!__traits(compiles, { test_dg(delegate C(C c) { return c; }); }));
880 static assert(!__traits(compiles, { I delegate(C) dg2 = dg; }));
882 // creates I delegate(C)
884 test_dg(delegate(C c) => c);
886 void test_fp(I function(C) fp)
889 void* cptr = cast(void*) c;
890 void* iptr = cast(void*) cast(I) c;
891 void* xptr = cast(void*) fp(c);
892 assert(cptr != iptr);
893 assert(cptr != xptr); // should pass
894 assert(iptr == xptr); // should pass
897 C function(C c) fp = function C(C c) { return c; };
898 static assert(!__traits(compiles, { test_fp(fp); }));
899 static assert(!__traits(compiles, { test_fp(function C(C c) { return c; }); }));
900 static assert(!__traits(compiles, { I function(C) fp2 = fp; }));
902 // creates I function(C)
904 test_fp(function(C c) => c);
907 /***************************************************/
908 // https://issues.dlang.org/show_bug.cgi?id=10288
912 void lambda() @trusted nothrow { x += 10; }
919 () @trusted { x += 10; } ();
926 () @trusted { x += g; } ();
930 void test10288() @safe pure nothrow
932 assert(foo10288(10) == 20); // OK
933 assert(bar10288(10) == 20); // OK <- NG
934 static assert(!__traits(compiles, baz10288(10)));
937 /***************************************************/
938 // https://issues.dlang.org/show_bug.cgi?id=10666
946 void foo10666(S10666 s1)
950 /* Even if closureVars(s1 and s2) are accessed by directly called lambdas,
951 * they won't escape the scope of this function.
953 auto x1 = (){ return s1.val; }(); // OK
954 auto x2 = (){ return s2.val; }(); // OK
957 /***************************************************/
958 // https://issues.dlang.org/show_bug.cgi?id=11081
960 T ifThrown11081(E : Throwable, T)(T delegate(E) errorHandler)
962 return errorHandler();
967 static if (__traits(compiles, ifThrown11081!Exception(e => 0)))
970 static if (__traits(compiles, ifThrown11081!Exception(e => 0)))
975 /***************************************************/
976 // https://issues.dlang.org/show_bug.cgi?id=11220
978 int parsePrimaryExp11220(int x)
980 parseAmbig11220!( (parsed){ x += 1; } )();
984 typeof(handler(1)) parseAmbig11220(alias handler)()
986 return handler(parsePrimaryExp11220(1));
989 /***************************************************/
990 // https://issues.dlang.org/show_bug.cgi?id=11230
992 template map11230(fun...)
994 auto map11230(Range)(Range r)
996 return MapResult11230!(fun, Range)(r);
1000 struct MapResult11230(alias fun, R)
1003 this(R input) { _input = input; }
1006 class A11230 { A11230[] as; }
1007 class B11230 { A11230[] as; }
1008 class C11230 : A11230 {}
1010 C11230 visit11230(A11230 a)
1012 a.as.map11230!(a => visit11230);
1015 C11230 visit11230(B11230 b)
1017 b.as.map11230!(a => visit11230);
1025 /***************************************************/
1026 // https://issues.dlang.org/show_bug.cgi?id=10336
1030 template opDispatch(string name)
1032 enum opDispatch = function(int x) {
1041 assert(s.hello(12) == 12);
1044 /***************************************************/
1045 // https://issues.dlang.org/show_bug.cgi?id=10928
1053 void f10928a(D10928 bar)
1057 void f10928b(D10928 bar) @nogc
1064 f10928a(D10928.init);
1065 f10928b(D10928.init);
1068 /***************************************************/
1069 // https://issues.dlang.org/show_bug.cgi?id=11661
1073 void delegate() dg = {}; // OK
1074 void function() fp = {}; // OK <- NG
1077 /***************************************************/
1078 // https://issues.dlang.org/show_bug.cgi?id=11774
1080 void f11774(T, R)(R delegate(T[]) dg)
1088 int[] delegate(int[]) dg = (int[] a) => a;
1090 f11774!Object(a => a);
1094 /***************************************************/
1095 // https://issues.dlang.org/show_bug.cgi?id=12421
1099 void test(string decl, bool polymorphic = true)()
1101 // parse AliasDeclaration in Statement
1102 mixin("alias f = " ~ decl ~ ";");
1104 static if (polymorphic)
1105 assert(f("s") == "s");
1107 // parse AliasDeclaration in DeclDefs
1108 mixin("template X() { alias g = " ~ decl ~ "; }");
1111 static if (polymorphic)
1112 assert(g("s") == "s");
1116 test!(q{ ( x) => x });
1117 test!(q{ (int x) => x }, false);
1119 test!(q{ ( x){ return x; } });
1120 test!(q{ (int x){ return x; } }, false);
1122 test!(q{ function ( x) => x });
1123 test!(q{ function (int x) => x }, false);
1124 test!(q{ function int ( x) => x }, false);
1125 test!(q{ function int (int x) => x }, false);
1127 test!(q{ delegate ( x) => x });
1128 test!(q{ delegate (int x) => x }, false);
1129 test!(q{ delegate int ( x) => x }, false);
1130 test!(q{ delegate int (int x) => x }, false);
1132 test!(q{ function ( x){ return x; } });
1133 test!(q{ function (int x){ return x; } }, false);
1134 test!(q{ function int ( x){ return x; } }, false);
1135 test!(q{ function int (int x){ return x; } }, false);
1137 test!(q{ delegate ( x){ return x; } });
1138 test!(q{ delegate (int x){ return x; } }, false);
1139 test!(q{ delegate int ( x){ return x; } }, false);
1140 test!(q{ delegate int (int x){ return x; } }, false);
1142 // This is problematic case, and should be disallowed in the future.
1148 /***************************************************/
1149 // https://issues.dlang.org/show_bug.cgi?id=12508
1156 class C12508 : A12508!double
1158 double getT() { return 1; }
1161 void f12508(A12508!double delegate() dg)
1165 assert(a.getT() == 1.0); // fails!
1168 void t12508(T)(A12508!T delegate() dg)
1172 assert(a.getT() == 1.0); // fails!
1175 ref alias Dg12508 = A12508!double delegate();
1176 void t12508(T)(Dg12508 dg) {}
1180 f12508({ return new C12508(); });
1181 t12508({ return new C12508(); });
1182 static assert(!__traits(compiles, x12508({ return new C12508(); })));
1185 /***************************************************/
1186 // https://issues.dlang.org/show_bug.cgi?id=13879
1190 bool function(int)[2] funcs1 = (int x) => true; // OK
1191 assert(funcs1[0] is funcs1[1]);
1192 funcs1[0] = x => true; // OK
1193 bool function(int)[2] funcs2 = x => true; // OK <- Error
1194 assert(funcs2[0] is funcs2[1]);
1197 /***************************************************/
1198 // https://issues.dlang.org/show_bug.cgi?id=14745
1202 // qualified nested functions
1203 auto foo1() pure immutable { return 0; }
1204 auto foo2() pure const { return 0; }
1206 // qualified lambdas (== implicitly marked as delegate literals)
1207 auto lambda1 = () pure immutable { return 0; };
1208 auto lambda2 = () pure const { return 0; };
1209 static assert(is(typeof(lambda1) : typeof(&foo1)));
1210 static assert(is(typeof(lambda2) : typeof(&foo2)));
1212 // qualified delegate literals
1213 auto dg1 = delegate () pure immutable { return 0; };
1214 auto dg2 = delegate () pure const { return 0; };
1215 static assert(is(typeof(dg1) : typeof(&foo1)));
1216 static assert(is(typeof(dg2) : typeof(&foo2)));
1219 /***************************************************/
1220 // https://issues.dlang.org/show_bug.cgi?id=15794
1224 static void fun(Holder)()
1226 int i = Holder.fn();
1230 struct Holder15794(alias Fn)
1235 void gun15794(alias fn, U...)()
1237 Foo15794.fun!(Holder15794!fn)();
1242 gun15794!(() => 0)(); // Line 26
1245 /***************************************************/
1246 // https://issues.dlang.org/show_bug.cgi?id=16271
1248 ref auto funa16271(alias dg, T)(ref T a)
1253 ref auto func16271(alias dg)()
1258 void assign16271(T)(ref T a, T b)
1260 alias fun = ref (ref a) => a;
1267 (ref () => x )() = 1; assert(x == 1);
1268 func16271!(ref () => x) = 2; assert(x == 2);
1269 assign16271(x, 3); assert(x == 3);
1271 alias alx = func16271!(ref () => x);
1272 alx = 4; assert(x == 4);
1274 alias alf = ref (ref a) => a;
1275 auto auf = ref (ref int a) => a;
1276 alf(x) = 5; assert(x == 5);
1277 auf(x) = 6; assert(x == 6);
1279 assert((funa16271!( ref (ref a) => a)(x) += 1) == 7 );
1280 assert((funa16271!(function ref (ref a) => a)(x) += 1) == 8 );
1281 assert((funa16271!(function ref int(ref a) => a)(x) += 1) == 9 );
1282 assert((funa16271!(delegate ref (ref a) => a)(x) += 1) == 10);
1283 assert((funa16271!(delegate ref int(ref a) => a)(x) += 1) == 11);
1286 alias aldc = ref () @trusted @nogc { return x; };
1287 auto audc = ref () @safe nothrow { return x; };
1288 alias alfuc = function ref (ref x) @trusted { return x; };
1289 alias aldec = delegate ref () @trusted { return x; };
1290 aldc() = 12; assert(x == 12);
1291 audc() = 13; assert(x == 13);
1292 alfuc(x) = 14; assert(x == 14);
1293 aldec() = 15; assert(x == 15);
1298 alias alf = ref () => x;
1299 auto auf = ref () => x;
1301 T!().alf() = 1; assert(T!().x == 1);
1302 T!().auf() = 2; assert(T!().x == 2);
1305 /***************************************************/
1365 printf("Success\n");