@@ -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,45 @@ 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
+ writeln("v: ", v);
460
+ }
461
+
462
+ void main() {
463
+ fun(); // prints v: 6
464
+
465
+ alias Foo = void function(int=7);
466
+ Foo foo = &fun;
467
+ foo(); // prints v: 7
468
+ foo(8); // prints v: 8
469
+ }
470
+ -----------
471
+ )
453
472
$(SPEC_RUNNABLE_EXAMPLE_RUN
454
473
-----------
455
474
import std.stdio : writefln;
456
475
457
476
void main() {
458
- Foo foo = &foofoo;
459
- foo(); // prints v: 6
460
- foo(8); // prints v: 8
461
- Bar bar = &barbar;
477
+ fun(4); // prints a: 4, b: 6, c: 7
478
+
479
+ Bar bar = &fun;
462
480
//bar(4); // compilation error, because the `Bar` alias
463
481
// requires an explicit 2nd argument
464
- barbar(4); // prints a: 4, b: 6, c: 7
465
482
bar(4, 5); // prints a: 4, b: 5, c: 9
466
483
bar(4, 5, 6); // prints a: 4, b: 5, c: 6
467
484
468
- Baz baz = &barbar ;
485
+ Baz baz = &fun ;
469
486
baz(); // prints a: 2, b: 3, c: 4
470
487
}
471
488
472
- alias Foo = void function(int=6);
473
489
alias Bar = void function(int, int, int=9);
474
490
alias Baz = void function(int=2, int=3, int=4);
475
491
476
- void foofoo(int v = 6) {
477
- writefln("v: %d", v);
478
- }
479
-
480
- void barbar(int a, int b = 6, int c = 7) {
492
+ void fun(int a, int b = 6, int c = 7) {
481
493
writefln("a: %d, b: %d, c: %d", a, b, c);
482
494
}
483
495
-----------
0 commit comments