Skip to content

Commit a36729c

Browse files
authored
Improve function (pointer) type docs (#3706)
Fix Issue 24210 - Function types are not documented Make examples runnable. Use list for kinds of delegate. Remove C pointer to function syntax as it's no longer supported.
1 parent fba4abc commit a36729c

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

spec/function.dd

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,9 +3135,17 @@ $(H2 $(LNAME2 function-pointers-delegates, Function Pointers, Delegates and Clos
31353135

31363136
$(H3 $(LNAME2 function-pointers, Function Pointers))
31373137

3138-
$(P A function pointer can point to a static nested function:)
3138+
$(P A function pointer is declared with the `function` keyword:)
31393139

31403140
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
3141+
---
3142+
void f(int);
3143+
void function(int) fp = &f; // fp is a pointer to a function taking an int
3144+
---
3145+
)
3146+
$(P A function pointer can point to a static nested function:)
3147+
3148+
$(SPEC_RUNNABLE_EXAMPLE_RUN
31413149
------
31423150
int function() fp; // fp is a pointer to a function returning an int
31433151

@@ -3149,10 +3157,12 @@ void test()
31493157
fp = &foo;
31503158
}
31513159

3152-
void bar()
3160+
void main()
31533161
{
3162+
assert(!fp);
31543163
test();
3155-
int i = fp(); // i is set to 10
3164+
int i = fp();
3165+
assert(i == 10);
31563166
}
31573167
------
31583168
)

spec/type.dd

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ $(H2 $(LEGACY_LNAME2 Derived Data Types, derived-data-types, Derived Data Types)
140140
$(LI $(DDSUBLINK spec/arrays, static-arrays, Static Arrays))
141141
$(LI $(DDSUBLINK spec/arrays, dynamic-arrays, Dynamic Arrays))
142142
$(LI $(DDLINK spec/hash-map, Associative Array, Associative Arrays))
143-
$(LI $(DDLINK spec/function, Functions, Functions))
144-
$(LI $(RELATIVE_LINK2 delegates, Delegates))
143+
$(LI $(RELATIVE_LINK2 functions, Function Types))
144+
$(LI $(RELATIVE_LINK2 delegates, Delegate Types))
145145
)
146146

147147
$(H3 $(LNAME2 pointers, Pointers))
@@ -548,36 +548,62 @@ values `false` and `true`, respectively. Casting an expression to `bool` means
548548
testing for `0` or `!=0` for arithmetic types, and `null` or `!=null` for
549549
pointers or references.)
550550

551-
$(H2 $(LNAME2 delegates, Delegates))
552551

553-
$(P Delegates are an aggregate of two pieces of data: an
554-
object reference and a pointer to a non-static member function, or a pointer to
555-
a closure and a pointer to a nested function. The object reference forms the
556-
`this` pointer when the function is called.)
552+
$(H2 $(LNAME2 functions, Function Types))
557553

558-
$(P Delegates are declared similarly to function pointers:)
554+
$(P A function type has the form:)
555+
556+
$(GRAMMAR
557+
$(GLINK TypeCtor)$(OPT) $(GLINK BasicType) $(GLINK2 function, Parameters) $(GLINK2 function, FunctionAttributes)$(OPT)
558+
)
559+
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).)
562+
563+
$(P Instantiating a function type is illegal. Instead, a pointer to function
564+
or delegate can be used. Those have these type forms respectively:)
565+
566+
$(GRAMMAR
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)
569+
)
559570

560571
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
561-
-------------------
562-
int function(int) fp; // fp is pointer to a function
563-
int delegate(int) dg; // dg is a delegate to a function
564-
-------------------
572+
---
573+
void f(int);
574+
pragma(msg, typeof(f)); // void(int)
575+
pragma(msg, typeof(&f)); // void function(int)
576+
---
565577
)
566578

567-
$(P A delegate is initialized analogously to function pointers:
568-
)
579+
$(P See $(DDSUBLINK spec/function, function-pointers, Function Pointers).)
569580

581+
$(H3 $(LNAME2 delegates, Delegates))
582+
583+
$(P Delegates are an aggregate of two pieces of data, either:)
584+
* An object reference and a pointer to a non-static
585+
$(DDSUBLINK spec/class, member-functions, member function).
586+
* A pointer to a closure and a pointer to a
587+
$(DDSUBLINK spec/function, nested, nested function).
588+
The object reference forms the `this` pointer when the function is called.)
589+
590+
$(P Delegates are declared and initialized similarly to function pointers:)
591+
592+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
570593
-------------------
571-
int func(int);
572-
fp = &func; // fp points to func
594+
int delegate(int) dg; // dg is a delegate to a function
595+
573596
class OB
574597
{
575598
int member(int);
576599
}
577-
OB o;
578-
dg = &o.member; // dg is a delegate to object o and
579-
// member function member
600+
601+
void f(OB o)
602+
{
603+
dg = &o.member; // dg is a delegate to object o and member function member
604+
}
580605
-------------------
606+
)
581607

582608
$(P Delegates cannot be initialized with static member functions
583609
or non-member functions.
@@ -609,12 +635,6 @@ mfp(c, 1); // and call c.foo(1)
609635
---
610636
)
611637

612-
$(P The C style syntax for declaring pointers to functions is deprecated:)
613-
614-
-------------------
615-
int (*fp)(int); // fp is pointer to a function
616-
-------------------
617-
618638
$(H2 $(LNAME2 typeof, $(D typeof)))
619639

620640
$(GRAMMAR

0 commit comments

Comments
 (0)