Reply to topic  [ 4 posts ] 
Recursive use 
Author Message
Yorick Master

Joined: Wed Jun 01, 2005 11:34 am
Posts: 112
Post Recursive use
Consider this code:

Code:
func test1(nil) {
  use, data;
  grow, data, "test1";
}

func test2(nil) {
  use, data;
  noop, use(test1,);
  grow, data, "test2";
  noop, use(test1,);
}

test = save(data=["start"], test1, test2);


And then this interactive session:
Code:
> test.data
["start"]
> test, test1
> test.data
["start","test1"]
> test, test2
> test.data
["start","test1","test2"]
>


I frequently stumble on this as a gotcha. What I want for that final check on "test.data" is ["start","test1","test1","test2","test1"].

Now, I understand exactly why this doesn't work the way I want. The documentation for "use" says:

Quote:
In the first form, the VARi must be extern to the calling function, and you get read-write access to VARi. That is, if you redefine any of the VARi, your changes will be saved back to the context object when the calling function returns.


So in pseudo-code, the call to test2 does the following:

Load variable "data" from group as Copy1
Call test1:
- Load variable "data" from group as Copy2
- Modify Copy2
- Return, saving Copy2 to group as "data"
Modify Copy1
Call test1:
- Load variable "data" from group as Copy3
- Modify Copy3
- Return, saving Copy3 to group as "data"
Return, saving Copy1 to group as "data"

So basically, what happens within test1 has no impact on test2 because it's working on a separate copy of the data, which it then saves back to "data", clobbering what test1 did.

My question is, is there a way I can use the convenience of "use" without breaking the ability to call other routines within the object that also use "use"? Or conversely, would it be possible to change the behavior so that use'd variables are updated in the group as they're changed?


Mon Feb 13, 2012 3:50 pm
Profile
Yorick Guru

Joined: Thu May 10, 2007 12:07 pm
Posts: 62
Post Re: Recursive use
This should do what you want.


Code:
func test1(nil) {
  use, data;
  grow, data, "test1";
}

func test2(nil) {
  u = use();
  u, test1,;
  save, u, "data", grow( u(data) , "test2");
  u, test1;
}

test = save(data=["start"], test1, test2);

Personally I always use the oxy object in the form oxy = use(); then I do what ever i need to do with 'oxy', I found it less confusing for me.


Sat Feb 18, 2012 5:11 pm
Profile
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Re: Recursive use
I agree with dnagle; this is an unintentional misfeature of my use function. I'll see if I can fix it.

The point at issue is whether or not it is possible to invoke an object method from another method of the same object. If both methods are only reading other object members, this works as expected, but when they are modifying other object members, the subroutine form of the use function gets confused. As sguieu points out, you can work around this by using the functional form of use, but that kind of defeats the purpose of the subroutine form.

Code:
func meth1(x,y) {
  use, a;
  a = x;
  noop, use(meth2, y);
}
func meth2(x) {
  use, a, b;
  b = a + x;
  a = x;
}
obj = save(meth1, meth2, a=2, b=3);
obj, meth1, 5, 7;


ought to wind up with obj.a=7 and obj.b=12, not obj.a=5 and obj.b=3. It may be possible to make this happen; if so, I'll change the use function to do so.


Wed Feb 22, 2012 2:33 pm
Profile
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Re: Recursive use
I think these issues are addressed now. See http://yorick.sourceforge.net/phpBB3/viewtopic.php?f=7&t=363.


Fri Mar 16, 2012 6:42 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.