@@ -3144,7 +3144,9 @@ void main()
31443144
31453145 $(P The stack variables referenced by a nested function are
31463146 still valid even after the function exits (NOTE this is different
3147- from D 1.0.) This is called a $(I closure).
3147+ from D 1.0).
3148+ This combining of the environment and the function is called
3149+ a $(I dynamic closure).
31483150 )
31493151
31503152 $(P Those referenced stack variables that make up the closure
@@ -3185,6 +3187,8 @@ void main()
31853187 a closure and is an error.
31863188 )
31873189
3190+ $(H4 $(LNAME2 method-delegates, Method Delegates))
3191+
31883192 $(P Delegates to non-static nested functions contain two pieces of
31893193 data: the pointer to the stack frame of the lexically enclosing
31903194 function (called the $(I context pointer)) and the address of the
@@ -3194,39 +3198,40 @@ void main()
31943198 Both forms of delegates are indistinguishable, and are
31953199 the same type.
31963200 )
3201+ $(P A delegate can be set to a particular object and method using `&obj.method`:
31973202
31983203$(SPEC_RUNNABLE_EXAMPLE_RUN
31993204------
32003205struct Foo
32013206{
3202- int a = 7 ;
3203- int bar () { return a; }
3207+ int a;
3208+ int get () { return a; }
32043209}
32053210
3206- int foo (int delegate() dg)
3211+ int add1 (int delegate() dg)
32073212{
32083213 return dg() + 1;
32093214}
32103215
32113216void main()
32123217{
3213- Foo f;
3214- int i = foo(&f.bar);
3218+ Foo f = {7};
3219+ int delegate() dg = &f.get; // bind to an instance of Foo and a method
3220+ assert(dg.ptr == &f);
3221+ assert(dg.funcptr == &Foo.get);
3222+
3223+ int i = add1(dg);
32153224 assert(i == 8);
32163225
32173226 int x = 27;
32183227 int abc() { return x; }
32193228
3220- i = foo (&abc);
3229+ i = add1 (&abc);
32213230 assert(i == 28);
32223231}
32233232------
32243233)
32253234
3226- $(P This combining of the environment and the function is called
3227- a $(I dynamic closure).
3228- )
3229-
32303235 $(P The $(D .ptr) property of a delegate will return the
32313236 $(I context pointer) value as a $(D void*).
32323237 )
0 commit comments