[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing zero as a Parameter/ NOT KEYWORD_SET



David Fanning wrote:
> Most people use Keyword_Set for entirely the wrong thing.
> Keyword_Set very clearly returns 0 if the argument is 0 or
> undefined. It returns a 1 if the argument is *anything*
> other than 0 or undefined. It should be used ONLY with
> those keywords that require a Boolean value.
> 
> But I welcome your comments on the article. (Just don't
> bug me night and day until I incorporate your comments. :-)
> 
>    http://www.dfanning.com/tips/keyword_check.html
> 

I just took a look at your article again, and I fear I must take issue
with the very first statement:

"Despite what your colleague may have told you, or what you believe you
read in the IDL documentation, it is NOT possible to reliably determine
if a keyword
was used in a call to your program."

I disagree.  Though I would never do this myself, you can easily get
this behaviour, vz.

pro testme, KEY=k
   if n_elements(k) ne 0 OR arg_present(k) then  $
    print,'You used KEY!' else $
    print,'You neglected KEY!'
end

For it must be either that k is undefined by virtue of not being passed
at all, or by virtue of being an as-yet undefined (and therefore
by-reference) variable passed in from above.  The former case we can
detect with n_elements(), the latter case with arg_present().  I
encourage you to try to find an example of something which is both
undefined, and also passed by value.  This would be the only thing which
could escape detection in the above algorithm.

When I first urged RSI to give us arg_present(), I had in mind exactly
the type of application you mention at the end of your page.  Your
strenuous warning might disincline readers from its use, but it really
has proven invaluable.  

I *don't* recommend using the above method, since you end up with all
sorts of undefined variables which are silently created and unused. 
This is not likely what the user expects.  It does open the possibility
for using keyword arguments as strings without being strings, e.g.

mypro, KEY=TRUE

but this is rather silly, I'd say, when you can just as easily use
/KEY.  But since you made such a strong point about it being impossible,
I couldn't keep it to myself :).

The rest of the page makes good sense though.  I hope this doesn't
qualify as bugging you day and night ;)

JD

-- 
 J.D. Smith                             |*|      WORK: (607) 255-5842    
 Cornell University Dept. of Astronomy  |*|            (607) 255-6263
 304 Space Sciences Bldg.               |*|       FAX: (607) 255-5875 
 Ithaca, NY 14853                       |*|