Skip to content

Commit 7abae85

Browse files
committed
[spec] Tweak function (pointer) type docs
Link to function types. Split function pointer example into 2, rename functions. Show relation between function type and function pointer type.
1 parent 92fee51 commit 7abae85

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

spec/declaration.dd

Lines changed: 26 additions & 14 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,45 @@ 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+
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+
)
453472
$(SPEC_RUNNABLE_EXAMPLE_RUN
454473
-----------
455474
import std.stdio : writefln;
456475

457476
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;
462480
//bar(4); // compilation error, because the `Bar` alias
463481
// requires an explicit 2nd argument
464-
barbar(4); // prints a: 4, b: 6, c: 7
465482
bar(4, 5); // prints a: 4, b: 5, c: 9
466483
bar(4, 5, 6); // prints a: 4, b: 5, c: 6
467484

468-
Baz baz = &barbar;
485+
Baz baz = &fun;
469486
baz(); // prints a: 2, b: 3, c: 4
470487
}
471488

472-
alias Foo = void function(int=6);
473489
alias Bar = void function(int, int, int=9);
474490
alias Baz = void function(int=2, int=3, int=4);
475491

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) {
481493
writefln("a: %d, b: %d, c: %d", a, b, c);
482494
}
483495
-----------

spec/type.dd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,9 @@ $(GLINK TypeCtor)$(OPT) $(GLINK BasicType) $(GLINK TypeSuffixes) `delegate` $(GL
571571
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
572572
---
573573
void f(int);
574-
pragma(msg, typeof(f)); // void(int)
575-
pragma(msg, typeof(&f)); // void function(int)
574+
alias Fun = void(int);
575+
static assert(is(typeof(f) == Fun));
576+
static assert(is(Fun* == void function(int)));
576577
---
577578
)
578579

0 commit comments

Comments
 (0)