@@ -433,11 +433,12 @@ assert(S.j == 4);
433
433
434
434
$(H3 $(LNAME2 alias-function, Aliasing a Function Type))
435
435
436
- $(P Function types can be aliased:)
436
+ $(P $(DDSUBLINK spec/type, functions, Function types) can be
437
+ aliased:)
437
438
438
439
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
439
440
---
440
- alias Fun = int(string p );
441
+ alias Fun = int(string);
441
442
int fun(string) {return 0;}
442
443
static assert(is(typeof(fun) == Fun));
443
444
@@ -450,34 +451,49 @@ static assert(is(MemberFun1 == MemberFun2));
450
451
$(P Type aliases can be used to call a function with different default
451
452
arguments, change an argument from required to default or vice versa:)
452
453
454
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
455
+ -----------
456
+ import std.stdio : writeln;
457
+
458
+ void fun(int v = 6)
459
+ {
460
+ writeln("v: ", v);
461
+ }
462
+
463
+ void main()
464
+ {
465
+ fun(); // prints v: 6
466
+
467
+ alias Foo = void function(int=7);
468
+ Foo foo = &fun;
469
+ foo(); // prints v: 7
470
+ foo(8); // prints v: 8
471
+ }
472
+ -----------
473
+ )
453
474
$(SPEC_RUNNABLE_EXAMPLE_RUN
454
475
-----------
455
476
import std.stdio : writefln;
456
477
457
- void main() {
458
- Foo foo = &foofoo;
459
- foo( ); // prints v: 6
460
- foo(8); // prints v: 8
461
- Bar bar = &barbar ;
478
+ void main()
479
+ {
480
+ fun(4 ); // prints a: 4, b: 6, c: 7
481
+
482
+ Bar bar = &fun ;
462
483
//bar(4); // compilation error, because the `Bar` alias
463
484
// requires an explicit 2nd argument
464
- barbar(4); // prints a: 4, b: 6, c: 7
465
485
bar(4, 5); // prints a: 4, b: 5, c: 9
466
486
bar(4, 5, 6); // prints a: 4, b: 5, c: 6
467
487
468
- Baz baz = &barbar ;
488
+ Baz baz = &fun ;
469
489
baz(); // prints a: 2, b: 3, c: 4
470
490
}
471
491
472
- alias Foo = void function(int=6);
473
492
alias Bar = void function(int, int, int=9);
474
493
alias Baz = void function(int=2, int=3, int=4);
475
494
476
- void foofoo(int v = 6) {
477
- writefln("v: %d", v);
478
- }
479
-
480
- void barbar(int a, int b = 6, int c = 7) {
495
+ void fun(int a, int b = 6, int c = 7)
496
+ {
481
497
writefln("a: %d, b: %d, c: %d", a, b, c);
482
498
}
483
499
-----------
0 commit comments