real simple delegate question.
angel via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 20 01:31:53 PDT 2017
On Saturday, 19 August 2017 at 18:33:37 UTC, WhatMeWorry wrote:
> On Friday, 18 August 2017 at 20:39:38 UTC, angel wrote:
>> On Friday, 18 August 2017 at 02:38:15 UTC, WhatMeForget wrote:
>>> [...]
>>
>> This actually appears correct ...
>> The 1-st example:
>> Each call to makeCalculator() increments a static (i.e. shared
>> among all makeCalculator() instances) variable - context.
>> In addition, makeCalculator() generates a random variable.
>> Whereas the delegate merely captures these variables, and the
>> displayed results reflect this.
>>
>> The 2-nd example:
>> There is a single call to makeCalculator().
>> After this call, context == 1, randy == _apparently 2_.
>> Now the delegate, as has already been said, merely captures
>> these values, so consecutive calls do not change the result.
>
> Thanks. So,
> auto calculator = makeCalculator();
> is the actual call of the delegate? "Delegate is function
> pointer with context"
> But what is
> ...calculator(0));
>
> Or maybe another approach would be to ask, what type is the
> compiler replacing auto with.
No !
The actual call to the delegate is calculator(0).
But this delegate does not induce change on its context
variables, so it is expectable that consecutive calls to
calculator(0) produce the same results, isn't it ?
makeCalculator(), while not a delegate, also has a context
variable - "static int context" - this is an "old-school" context
variable implemented by the means of static variable.
Consecutive calls to makeCalculator() return delegates having
different contexts, so each call to calculator(0) produces
different results.
More information about the Digitalmars-d-learn
mailing list