Skip to content

Commit 6281747

Browse files
authored
Merge pull request #4302 from ntrel/delegates2
[spec] Improve delegate docs Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents 37a5d89 + 0f5ca86 commit 6281747

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

spec/function.dd

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,7 +3278,8 @@ uint function(uint) fp2 = &def;
32783278

32793279
$(H3 $(LNAME2 closures, Delegates & Closures))
32803280

3281-
$(P A delegate can be set to a non-static nested function:)
3281+
$(P A $(DDSUBLINK spec/type, delegates, delegate) can be set to a
3282+
non-static $(RELATIVE_LINK2 nested, nested function):)
32823283

32833284
$(SPEC_RUNNABLE_EXAMPLE_RUN
32843285
------
@@ -3312,8 +3313,9 @@ void main()
33123313
$(P Those referenced stack variables that make up the closure
33133314
are allocated on the GC heap, unless:)
33143315

3315-
* The closure is passed to a `scope` parameter.
3316-
* The closure is an initializer for a `scope` variable.
3316+
* The closure is passed to a $(RELATIVE_LINK2 scope-parameters, `scope` parameter).
3317+
* The closure is an initializer for a
3318+
$(DDSUBLINK spec/attribute, scope, `scope` variable).
33173319
* The closure is assigned to a `scope` variable.
33183320

33193321
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -3347,7 +3349,7 @@ void main()
33473349
a closure and is an error.
33483350
)
33493351

3350-
$(H4 $(LNAME2 method-delegates, Method Delegates))
3352+
$(H3 $(LNAME2 method-delegates, Method Delegates))
33513353

33523354
$(P Delegates to non-static nested functions contain two pieces of
33533355
data: the pointer to the stack frame of the lexically enclosing
@@ -3377,9 +3379,6 @@ void main()
33773379
{
33783380
Foo f = {7};
33793381
int delegate() dg = &f.get; // bind to an instance of Foo and a method
3380-
assert(dg.ptr == &f);
3381-
assert(dg.funcptr == &Foo.get);
3382-
33833382
int i = add1(dg);
33843383
assert(i == 8);
33853384

@@ -3392,14 +3391,6 @@ void main()
33923391
------
33933392
)
33943393

3395-
$(P The $(D .ptr) property of a delegate will return the
3396-
$(I context pointer) value as a $(D void*).
3397-
)
3398-
3399-
$(P The $(D .funcptr) property of a delegate will return the
3400-
$(I function pointer) value as a function type.
3401-
)
3402-
34033394
$(H3 $(LNAME2 function-delegate-init, Initialization))
34043395

34053396
$(P Function pointers are zero-initialized by default.

spec/type.dd

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

714726
class 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)
735755
dg(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

Comments
 (0)