exist gotcha? (was Re: Re: How to tell a function from a string)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

exist gotcha? (was Re: Re: How to tell a function from a string)

Jonathan King-3
I *think* I've got this text attributed correctly...

John W. Eaton <[hidden email]> writes:

>
>On 20-Jan-1998, Jim Van Zandt <[hidden email]> wrote:
>|
>| I also thought the documentation could have been more helpful.
>| Eventually I found this:
>|
>|  - Built-in Function:  exist (NAME)
>|      Return 1 if the name exists as a variable, 2 if the name (after
>|      appending `.m') is a function file in the path, 3 if the name is a
>|      `.oct' file in the path, or 5 if the name is a built-in function.
>|      Otherwise, return 0.
>|
>| and decided that "exist(NAME) > 1" was a reasonable test.  (Can it
>| ever return 4?)
>
>No, Octave's exist function doesn't currently return 4, but I'm not
>sure what Matlab uses that return code for, so if you want to write
>portable code, you might want to check that first.

Matlab4.2 allegedly returns 4 if A is a compiled SIMULINK(tm)
function.  I say "allegedly" only because I haven't tried it.

>| I'm now surprised to read JWE's reply:
>|
>| > Unfortunately, there isn't a good way to do this from Octave, though
>| > you could probably wrap is_valid function from src/variables.cc in a
>| > DEFUN easily enough.
>|
>| What's wrong with the above test?
>
>Uh, I just completely forgot about it.

I see one other "gotcha", as suggested by the following octave session:

mk26:~/matlab> octave
Octave, version 2.0.9 (i586-pc-linux-gnu).
Copyright (C) 1996, 1997 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.

octave.bin:1> exist("who")  % this will return 5, like it should
ans = 5
octave.bin:2> % here's a stupid typo and its parse error
octave.bin:2> exist(who)
parse error:

>>> exist(who)
             ^

octave.bin:2> exist("who")  % now this will fail...
ans = 0
octave.bin:3> who           % until you use it correctly...
octave.bin:4> exist("who")  % and now it works again
ans = 5

I'm not sure what's going on here, but it looks funny.  As far as I
can tell, this only happens with built-ins; specifically, if you
precede the octave session with "touch who.m" in a directory on your
path, the parse error doesn't seem to block the correct operation of
exist (in the sense that it will correctly return ans=2, since it
notices the shadowing function).

I think I stumbled across this first when I accidentally defined a
variable called "save"...but I think that's all I want to say about
that particular incident.

jking


Reply | Threaded
Open this post in threaded view
|

exist gotcha? (was Re: Re: How to tell a function from a string)

John W. Eaton-6
On 20-Jan-1998, Jonathan King <[hidden email]> wrote:

| I see one other "gotcha", as suggested by the following octave session:
|
| mk26:~/matlab> octave
| Octave, version 2.0.9 (i586-pc-linux-gnu).
| Copyright (C) 1996, 1997 John W. Eaton.
| This is free software with ABSOLUTELY NO WARRANTY.
| For details, type `warranty'.
|
| octave.bin:1> exist("who")  % this will return 5, like it should
| ans = 5
| octave.bin:2> % here's a stupid typo and its parse error
| octave.bin:2> exist(who)
| parse error:
|
| >>> exist(who)
|              ^
|
| octave.bin:2> exist("who")  % now this will fail...
| ans = 0
| octave.bin:3> who           % until you use it correctly...
| octave.bin:4> exist("who")  % and now it works again
| ans = 5
|
| I'm not sure what's going on here, but it looks funny.

Yup.  Please try the following patch.

Thanks,

jwe


Tue Jan 20 17:02:19 1998  John W. Eaton  <[hidden email]>

        * variables.cc (Fexist): If local symbol is undefined, check
        global table.


*** src/variables.cc~ Tue Jan 13 13:03:12 1998
--- src/variables.cc Tue Jan 20 22:26:58 1998
***************
*** 351,357 ****
      }
 
    symbol_record *sr = curr_sym_tab->lookup (symbol_name, 0, 0);
!   if (! sr)
      sr = global_sym_tab->lookup (symbol_name, 0, 0);
 
    retval = 0.0;
--- 351,357 ----
      }
 
    symbol_record *sr = curr_sym_tab->lookup (symbol_name, 0, 0);
!   if (! (sr && sr->is_defined ()))
      sr = global_sym_tab->lookup (symbol_name, 0, 0);
 
    retval = 0.0;