www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - real simple delegate question.

reply WhatMeForget <kheaser gmail.com> writes:
Can someone explain what is the difference between the two? 
Thanks.

module gates;
import std.stdio;
import std.random;

alias Calculator = int delegate(int);

Calculator makeCalculator()
{
     static int context = 0;
     int randy = uniform(1, 7);
     context++;
     writeln("context = ", context);
     writeln("randy = ", randy);
     return value => context + randy + value;
}

void main()
{
     for (int i = 0; i < 3; i++)
     {
         auto calculator = makeCalculator();
         writeln("The result of the calculation: ", calculator(0));
     }
}
returns:
context = 1
randy = 5
The result of the calculation: 6
context = 2
randy = 2
The result of the calculation: 4
context = 3
randy = 6
The result of the calculation: 9

while the following

void main()
{
     auto calculator = makeCalculator();  // thought just one 
would work
     for (int i = 0; i < 3; i++)
     {
         writeln("The result of the calculation: ", calculator(0));
     }
}
returns:
The result of the calculation: 3
The result of the calculation: 3
The result of the calculation: 3
Aug 17
parent reply angel <andrey.gelman gmail.com> writes:
On Friday, 18 August 2017 at 02:38:15 UTC, WhatMeForget wrote:
 Can someone explain what is the difference between the two? 
 Thanks.

 module gates;
 import std.stdio;
 import std.random;

 alias Calculator = int delegate(int);

 Calculator makeCalculator()
 {
     static int context = 0;
     int randy = uniform(1, 7);
     context++;
     writeln("context = ", context);
     writeln("randy = ", randy);
     return value => context + randy + value;
 }

 void main()
 {
     for (int i = 0; i < 3; i++)
     {
         auto calculator = makeCalculator();
         writeln("The result of the calculation: ", 
 calculator(0));
     }
 }
 returns:
 context = 1
 randy = 5
 The result of the calculation: 6
 context = 2
 randy = 2
 The result of the calculation: 4
 context = 3
 randy = 6
 The result of the calculation: 9

 while the following

 void main()
 {
     auto calculator = makeCalculator();  // thought just one 
 would work
     for (int i = 0; i < 3; i++)
     {
         writeln("The result of the calculation: ", 
 calculator(0));
     }
 }
 returns:
 The result of the calculation: 3
 The result of the calculation: 3
 The result of the calculation: 3
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.
Aug 18
parent reply WhatMeWorry <kheaser gmail.com> writes:
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.
Aug 19
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 19 August 2017 at 18:33:37 UTC, WhatMeWorry wrote:
 Or maybe another approach would be to ask, what type is the 
 compiler replacing auto with.
If you want to find out compile with `-vcg-ast`
Aug 19
prev sibling parent angel <andrey.gelman gmail.com> writes:
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.
Aug 20