@@ -140,8 +140,8 @@ $(H2 $(LEGACY_LNAME2 Derived Data Types, derived-data-types, Derived Data Types)
140
140
$(LI $(DDSUBLINK spec/arrays, static-arrays, Static Arrays))
141
141
$(LI $(DDSUBLINK spec/arrays, dynamic-arrays, Dynamic Arrays))
142
142
$(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 ))
145
145
)
146
146
147
147
$(H3 $(LNAME2 pointers, Pointers))
@@ -548,36 +548,62 @@ values `false` and `true`, respectively. Casting an expression to `bool` means
548
548
testing for `0` or `!=0` for arithmetic types, and `null` or `!=null` for
549
549
pointers or references.)
550
550
551
- $(H2 $(LNAME2 delegates, Delegates))
552
551
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))
557
553
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
+ )
559
570
560
571
$(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
+ ---
565
577
)
566
578
567
- $(P A delegate is initialized analogously to function pointers:
568
- )
579
+ $(P See $(DDSUBLINK spec/function, function-pointers, Function Pointers).)
569
580
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
570
593
-------------------
571
- int func (int);
572
- fp = &func; // fp points to func
594
+ int delegate (int) dg; // dg is a delegate to a function
595
+
573
596
class OB
574
597
{
575
598
int member(int);
576
599
}
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
+ }
580
605
-------------------
606
+ )
581
607
582
608
$(P Delegates cannot be initialized with static member functions
583
609
or non-member functions.
@@ -609,12 +635,6 @@ mfp(c, 1); // and call c.foo(1)
609
635
---
610
636
)
611
637
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
-
618
638
$(H2 $(LNAME2 typeof, $(D typeof)))
619
639
620
640
$(GRAMMAR
0 commit comments