Reply to topic  [ 2 posts ] 
Weird behavior with oxy object and external vars 
Author Message
Yorick Guru

Joined: Thu May 10, 2007 12:07 pm
Posts: 62
Post Weird behavior with oxy object and external vars
Get started :
Code:

func test(oxy) { one = oxy(one); } /* set a (normally) local var with the result of method one from  the oxy objec */
obj = save(one=1)  /*  create the oxy object with the method one */



Now let us do that :
Code:
> one = 0;  /*  This is an external  variable it should not change */
> test, obj;
> one
1

The external variable one is set to 1. It should not because normally 'one' in the function 'test' is a local variable (because not explicitly set as extern and set in 'test').
Setting explicitly 'one' as local in 'test' solve the problem, but this is not the normal yorick behavior.

Also defining test as
Code:
func test(oxy) { dummy = oxy(one); }

The problem is gone :
Code:
> one =0;
> dummy =0;
> test, obj;
> one
0
> dummy
0


So it happens only when the local variable have the same name than the called oxy member ! Easy to get around this bug, but can be very confusing when debugging

Cheers,
Sylvain.


Sun Feb 12, 2012 10:36 am
Profile
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Re: Weird behavior with oxy object and external vars
You are wrong.

The statement "one = oxy(one);" has always implicitly declared the variable "one" to be external, because you need a value for "one" to plug in to the RHS before you can set the LHS. Compare to "x = sin(x);" -- it doesn't make any sense unless you already have a value for x.

The yorick parser does not realize that "oxy" will be an oxy-object (it can't, because you could pass anything to the function), hence it doesn't realize that the argument "one" will not be used as a value, but just as a name. The implicit extern/local rules are purely textual; yorick doesn't know anything about argument types.

This isn't a bug, and it isn't even "weird". If you want to to clarify this in your code, always declare every variable either local or extern explicitly. But I don't recommend it. The implicit rules are clear enough once you stop thinking that yorick has any knowledge of the meaning of ANY symbol at parse time. Keywords (func, if, else, return, etc.) are the only words that have any particular meaning to the parser.


Wed Feb 22, 2012 1:04 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 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.