Reply to topic  [ 5 posts ] 
grow(), inside a function, will modify a global variable 
Author Message
Yorick Padawan

Joined: Tue Jun 07, 2011 1:46 pm
Posts: 32
Location: UNH
Post grow(), inside a function, will modify a global variable
It only works when you set the same variable equal to the "grown" variable. In this case, it is test2.

Code:
test = nil;
test1 = nil;
test2 = nil;
func bugglobal(void)
{
   test1 = grow(test, "test1");
   test2 = grow(test2, "test2");
}
print, test, test1, test2;

It should return
Quote:
[] [] ["test2"]


Is this a bug? Since I use the test2 case a lot, I have to set the variable (test2) to nil inside of the function before returning it or I will have values on top of unrelated values.

Ronak


Wed Mar 14, 2012 11:39 am
Profile
Yorick Master

Joined: Tue Mar 07, 2006 10:31 pm
Posts: 125
Location: Meudon, France
Post Re: grow(), inside a function, will modify a global variable
I don't know whether it's a bug or a feature, but you shouldn't set test2 to nil. Doing so does not solve the issue at all: it still modifies the value of the external variable named test2.

The right solution is to declare it as local. Also, it would be best to use the procedure form of grow for this case:

Code:
test = "before";
test1 = "before1";
test2 = "before2";
func bugglobal(void)
{
   local test2;
   test1 = grow(test, "after1");
   grow, test2, "after2";
}
print, test, test1, test2;

Output:
Quote:
"before" "before1" "before2"


PS: in addition take care that "nil" is not a special word in yorick. Using it as if is always meant [] will fire back sooner or later:

Code:
nil = "true";

func myfunc(void) {
  var = nil;
  print, var;
}
myfunc;

Output:
Quote:
["true"]


Last edited by thibaut on Thu Mar 15, 2012 6:07 am, edited 1 time in total.



Thu Mar 15, 2012 1:50 am
Profile WWW
Yorick Master

Joined: Tue Mar 07, 2006 10:31 pm
Posts: 125
Location: Meudon, France
Post Re: grow(), inside a function, will modify a global variable
thibaut wrote:
I don't know whether it's a bug or a feature,


Now I know: it's feature, and it's not only "grow" but any function. See http://yorick.sourceforge.net/phpBB3/viewtopic.php?f=6&t=356.

I believe that the point is that the scope of a variable is determined the first time Yorick need to access a variable. If it's an direct assignment, as in
Code:
var=value
var is automatically local. But if you use var, as in any of
Code:
thingy=var;
thingy=function(var);
procedure, var;
var(indices)=thingy;

then var is automatically external.

Now, Yorick processes the right hand side of an assignment before the left had side, which is pretty normal (you can't assign "value" to "var" before you have computed "value". You cannot send a letter by the post before you have written it). For instance, the two following lines are functionally equivalent (though the former out to be faster than the latter, and the former issues an error if var is already a local variable):
Code:
extern var;
var=var;


Thu Mar 15, 2012 2:20 am
Profile WWW
Yorick Padawan

Joined: Tue Jun 07, 2011 1:46 pm
Posts: 32
Location: UNH
Post Re: grow(), inside a function, will modify a global variable
Ah okay, so by setting it as local, it fixes that problem. Ok, thank you.


Thu Mar 15, 2012 12:32 pm
Profile
Yorick Master

Joined: Tue Mar 07, 2006 10:31 pm
Posts: 125
Location: Meudon, France
Post Re: grow(), inside a function, will modify a global variable
About setting your variable to nil: indeed, it's nearly equivalent to do
Code:
var = [];
grow, var, whatever

or
Code:
local var;
grow, var, whatever


Thu Mar 15, 2012 2:03 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 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.