From bug-octave-request at bevo dot che dot wisc dot edu Fri Apr 2 14:07:33 2004 Subject: Re: Inconsistency between global and persistent From: Quentin Spencer To: "John W. Eaton" CC: bug-octave at bevo dot che dot wisc dot edu Date: Fri, 02 Apr 2004 14:08:18 -0600 I rebuilt octave with these changes, and ran a function with code like this: persistent x if(isempty(x)) ... end and it still complains that 'x' is undefined when it gets to the isempty statement. John W. Eaton wrote: >On 2-Apr-2004, Quentin Spencer wrote: > >| If I recall correctly, the behavior of global was modified a while ago >| to be more MATLAB compatible. For example, declaring a variable to be >| global automatically initializes it to an empty matrix. Declaring a >| variable to be persistent does not do this. I am trying to write a >| function that saves time by only going through the initialization code >| the first time the function is called. Here's how I would do this in MATLAB: >| >| persistent x >| if(isempty(x)) >| {initialization code here} >| end >| >| With the current behavior (version 2.1.57) of persistent in octave, >| here's how I would do the same thing: >| >| persistent x = {initialization code here} >| >| The problem is that these two cases really can't be reconciled without >| having interpreter-specific code for each case. Initially I liked the >| octave implementation, but I can see benefits of the MATLAB >| implementation if the initialization code is long and involves multiple >| steps. Since we already have the MATLAB-like behavior for globals, would >| it be reasonable to modify persistent to behave the same way? > >Please try the following patch. > >Thanks, > >jwe > > >2004-04-02 John W. Eaton > > * pt-decl.cc (tree_static_command::do_init): Initialize to empty > matrix by default. > > >Index: pt-decl.cc >=================================================================== >RCS file: /usr/local/cvsroot/octave/src/pt-decl.cc,v >retrieving revision 1.18 >diff -u -r1.18 pt-decl.cc >--- a/pt-decl.cc 13 Jul 2003 23:15:29 -0000 1.18 >+++ b/pt-decl.cc 2 Apr 2004 16:56:43 -0000 > at @ -147,13 +147,18 @@ > { > id->mark_as_static (); > >- tree_expression *expr = elt.expression (); >+ octave_lvalue ult = id->lvalue (); > >- if (expr) >+ if (ult.is_defined ()) > { >- octave_value init_val = expr->rvalue (); >+ tree_expression *expr = elt.expression (); > >- octave_lvalue ult = id->lvalue (); >+ octave_value init_val; >+ >+ if (expr) >+ init_val = expr->rvalue (); >+ else >+ init_val = Matrix (); > > ult.assign (octave_value::op_asn_eq, init_val); > } > > > ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------