@@ -221,7 +221,7 @@ assert(i == 4);
221221
222222 $(BEST_PRACTICE Use a $(DDSUBLINK spec/function, ref-params,
223223 `ref` function parameter) or a $(DDSUBLINK spec/declaration, ref-variables,
224- `ref` local variable) when the address doesn't escape.
224+ `ref` local variable) when the address doesn't escape.)
225225
226226
227227$(H2 $(LEGACY_LNAME2 User Defined Types, user-defined-types, User-Defined Types))
@@ -698,27 +698,47 @@ $(P See $(DDSUBLINK spec/function, function-pointers, Function Pointers).)
698698
699699$(H3 $(LNAME2 delegates, Delegates))
700700
701- $(P Delegates are an aggregate of two pieces of data, either:)
701+ $(P Delegates are an aggregate of two pieces of data, a *context pointer* and
702+ a *function pointer*. A valid delegate holds either:)
703+
702704* An object reference and a pointer to a non-static
703705 $(DDSUBLINK spec/class, member-functions, member function).
704- * A pointer to a closure and a pointer to a
705- $(DDSUBLINK spec/function, nested, nested function).
706- The object reference forms the `this` pointer when the function is called.)
706+ The object reference forms the `this` pointer when the function is called.
707+ * A pointer to a $(DDSUBLINK spec/function, closures, closure)
708+ and a pointer to a $(DDSUBLINK spec/function, nested, nested function).
709+
710+ $(P The $(D .ptr) property of a delegate will return the
711+ $(I context pointer) value as a $(D void*).
712+ )
713+
714+ $(P The $(D .funcptr) property of a delegate will return the
715+ $(I function pointer) value as a function type.
716+ )
707717
708718$(P Delegates are declared and initialized similarly to function pointers:)
709719
710- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
720+ $(SPEC_RUNNABLE_EXAMPLE_RUN
711721-------------------
712- int delegate(int) dg; // dg is a delegate to a function
722+ void func(int) {}
723+ void function(int) fp; // fp is a function pointer
724+ void delegate(int) dg; // dg is a delegate to a function
713725
714726class OB
715727{
716- int member(int);
728+ void member(int) {}
717729}
718730
719- void f(OB o )
731+ void main( )
720732{
721- dg = &o.member; // dg is a delegate to object o and member function member
733+ OB o = new OB;
734+
735+ fp = &func; // fp points to function `func`
736+
737+ dg = &o.member; // dg is a delegate to object `o` and member function `member`
738+ assert(dg.ptr == cast(void*) o);
739+ assert(dg.funcptr == &OB.member);
740+
741+ dg = (int i) { o.member(i); }; // dg holds a delegate literal with main's execution context
722742}
723743-------------------
724744)
@@ -735,6 +755,11 @@ fp(3); // call func(3)
735755dg(3); // call o.member(3)
736756-------------------
737757
758+ $(P See:)
759+
760+ * $(DDSUBLINK spec/function, closures, Delegates and Closures)
761+ * $(DDSUBLINK spec/function, method-delegates, Method Delegates)
762+
738763 $(P The equivalent of member function pointers can be constructed
739764 using $(DDSUBLINK spec/expression, function_literals, anonymous lambda functions):)
740765
0 commit comments