@@ -3144,7 +3144,9 @@ void main()
3144
3144
3145
3145
$(P The stack variables referenced by a nested function are
3146
3146
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).
3148
3150
)
3149
3151
3150
3152
$(P Those referenced stack variables that make up the closure
@@ -3185,6 +3187,8 @@ void main()
3185
3187
a closure and is an error.
3186
3188
)
3187
3189
3190
+ $(H4 $(LNAME2 method-delegates, Method Delegates))
3191
+
3188
3192
$(P Delegates to non-static nested functions contain two pieces of
3189
3193
data: the pointer to the stack frame of the lexically enclosing
3190
3194
function (called the $(I context pointer)) and the address of the
@@ -3194,39 +3198,40 @@ void main()
3194
3198
Both forms of delegates are indistinguishable, and are
3195
3199
the same type.
3196
3200
)
3201
+ $(P A delegate can be set to a particular object and method using `&obj.method`:
3197
3202
3198
3203
$(SPEC_RUNNABLE_EXAMPLE_RUN
3199
3204
------
3200
3205
struct Foo
3201
3206
{
3202
- int a = 7 ;
3203
- int bar () { return a; }
3207
+ int a;
3208
+ int get () { return a; }
3204
3209
}
3205
3210
3206
- int foo (int delegate() dg)
3211
+ int add1 (int delegate() dg)
3207
3212
{
3208
3213
return dg() + 1;
3209
3214
}
3210
3215
3211
3216
void main()
3212
3217
{
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);
3215
3224
assert(i == 8);
3216
3225
3217
3226
int x = 27;
3218
3227
int abc() { return x; }
3219
3228
3220
- i = foo (&abc);
3229
+ i = add1 (&abc);
3221
3230
assert(i == 28);
3222
3231
}
3223
3232
------
3224
3233
)
3225
3234
3226
- $(P This combining of the environment and the function is called
3227
- a $(I dynamic closure).
3228
- )
3229
-
3230
3235
$(P The $(D .ptr) property of a delegate will return the
3231
3236
$(I context pointer) value as a $(D void*).
3232
3237
)
0 commit comments