www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Member functions with access to local variables

reply "Linden Krouse" <ztaticnull gmai.com> writes:
Hello! While working on a project of mine, I ran into a situation 
where it would be beneficial and elegant to call a function with 
access to the caller's local variables. Of course D supports this 
with nested functions but the function to be called needs to have 
access to the private variables of another object. In fact the 
function should be a member function of the second class, 
although then it would need to be passed many (6+) variables, 
most of which will not be needed in most cases. I know another 
solution would be sacrificing encapsulation and increase the 
visibility of the members of the second class but that seems much 
less elegant. Splitting the function into smaller functions is 
also not possible as what variables are needed is determined by 
the second object at runtime.
So...

Is there a way to have a function be both a nested function and a 
member function of a different class?

Perhaps something like

class A
{
     B b = new B();
     int foo()
     {
         int x = 0;
         b.bar();
         return x; //returns 5
     }
}

class B
{
     private int y = 5;
     void A.foo:bar()
     {
         x += y;
     }
}

if there isn't already a way to do this.
Apr 08 2013
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Monday, 8 April 2013 at 23:43:45 UTC, Linden Krouse wrote:
 Is there a way to have a function be both a nested function and 
 a member function of a different class?
You could use inheritance: class A { int i; int f() { return i; } } void main() { int j; class B : A { override int f() { return i + j; } } } In the future, consider posting such questions to digitalmars.D.learn.
Apr 09 2013
parent reply "Linden Krouse" <ztaticnull gmai.com> writes:
On Tuesday, 9 April 2013 at 12:22:56 UTC, Vladimir Panteleev 
wrote:
 On Monday, 8 April 2013 at 23:43:45 UTC, Linden Krouse wrote:
 Is there a way to have a function be both a nested function 
 and a member function of a different class?
You could use inheritance: class A { int i; int f() { return i; } } void main() { int j; class B : A { override int f() { return i + j; } } } In the future, consider posting such questions to digitalmars.D.learn.
Ah sorry, it was also kind of a suggestion but mostly a question. Also the second class is mostly unrelated to the first and they both inherit from another classes already.
Apr 09 2013
parent "Linden Krouse" <ztaticnull gmai.com> writes:
I suppose my initial example wasn't the greatest. Here's a better 
example of what I'm trying to do.

module a;
class A : SuperA
{
     B b = new B();
     C c;
     D d;
     //...

     this()
     {
         b.loadInteralStuff("filepathofstufftoload");
     }

     void doStuff()
     {
         int var1, var2;
         char var3;
         int[] var4;
         File var5;

         //read and parse stuff from a file...

         //found commands that need to be executed, although
         //this class only knows that they are a set of commands
         //not what they actually do (and shouldn't)
         b.executeCammands(commandArray);
     }

}

module b;
class B : SuperB
{
     SomeComplexDataStructure data;

     //private helper functions

     void loadInternalStuff(string filename)
     {
         //load a file in a certain way. This implementation
         //could be subject to change which shouldn't affect A
     }
     void executeCammands(byte[] commands)
     {
         //execute each command, most of which need to edit
         //the local variables of doStuff() but should be
         //implemented in this class

         //in most cases, the local variables needed could be
         //similar to any implementation but are different enough
         //for it to be a major annoyance to pass every possible
         //variable needed to this function by reference
     }
}

It would be convenient to have a way to either specify that a 
function can be called only by a specific function and have 
access to it's local variables or overload a function to provide 
a specific implementation for certain functions.
Apr 09 2013