Skip to content

Commit aa0a946

Browse files
authored
Merge pull request #3722 from ntrel/fn-type-alias
[spec] Improve function type (alias) docs Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents 92fee51 + cd22c4d commit aa0a946

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

spec/declaration.dd

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,12 @@ assert(S.j == 4);
433433

434434
$(H3 $(LNAME2 alias-function, Aliasing a Function Type))
435435

436-
$(P Function types can be aliased:)
436+
$(P $(DDSUBLINK spec/type, functions, Function types) can be
437+
aliased:)
437438

438439
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
439440
---
440-
alias Fun = int(string p);
441+
alias Fun = int(string);
441442
int fun(string) {return 0;}
442443
static assert(is(typeof(fun) == Fun));
443444

@@ -450,34 +451,49 @@ static assert(is(MemberFun1 == MemberFun2));
450451
$(P Type aliases can be used to call a function with different default
451452
arguments, change an argument from required to default or vice versa:)
452453

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+
)
453474
$(SPEC_RUNNABLE_EXAMPLE_RUN
454475
-----------
455476
import std.stdio : writefln;
456477

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;
462483
//bar(4); // compilation error, because the `Bar` alias
463484
// requires an explicit 2nd argument
464-
barbar(4); // prints a: 4, b: 6, c: 7
465485
bar(4, 5); // prints a: 4, b: 5, c: 9
466486
bar(4, 5, 6); // prints a: 4, b: 5, c: 6
467487

468-
Baz baz = &barbar;
488+
Baz baz = &fun;
469489
baz(); // prints a: 2, b: 3, c: 4
470490
}
471491

472-
alias Foo = void function(int=6);
473492
alias Bar = void function(int, int, int=9);
474493
alias Baz = void function(int=2, int=3, int=4);
475494

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+
{
481497
writefln("a: %d, b: %d, c: %d", a, b, c);
482498
}
483499
-----------

spec/type.dd

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,22 +557,24 @@ $(GRAMMAR_INFORMATIVE
557557
$(GLINK2 declaration, StorageClasses)$(OPT) $(GLINK Type) $(GLINK2 function, Parameters) $(GLINK2 function, FunctionAttributes)$(OPT)
558558
)
559559

560-
$(P A function type e.g. `int(int)` is only used for type tests.
561-
A function type $(DDSUBLINK spec/declaration, alias-function, can be aliased).)
560+
$(P Function types are not included in the $(GLINK Type) grammar.
561+
A function type e.g. `int(int)` $(DDSUBLINK spec/declaration, alias-function, can be aliased).
562+
A function type is only used for type tests or as the target type of a pointer.)
562563

563564
$(P Instantiating a function type is illegal. Instead, a pointer to function
564565
or delegate can be used. Those have these type forms respectively:)
565566

566567
$(GRAMMAR_INFORMATIVE
567-
$(GLINK TypeCtor)$(OPT) $(GLINK BasicType) $(GLINK TypeSuffixes) `function` $(GLINK2 function, Parameters) $(GLINK2 function, FunctionAttributes)$(OPT)
568-
$(GLINK TypeCtor)$(OPT) $(GLINK BasicType) $(GLINK TypeSuffixes) `delegate` $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT)
568+
$(GLINK Type) `function` $(GLINK2 function, Parameters) $(GLINK2 function, FunctionAttributes)$(OPT)
569+
$(GLINK Type) `delegate` $(GLINK2 function, Parameters) $(GLINK2 function, MemberFunctionAttributes)$(OPT)
569570
)
570571

571572
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
572573
---
573574
void f(int);
574-
pragma(msg, typeof(f)); // void(int)
575-
pragma(msg, typeof(&f)); // void function(int)
575+
alias Fun = void(int);
576+
static assert(is(typeof(f) == Fun));
577+
static assert(is(Fun* == void function(int)));
576578
---
577579
)
578580

0 commit comments

Comments
 (0)