[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Keyword precedence
"Mark Hadfield" <m.hadfield@niwa.cri.nz> writes:
> It's interesting that David Fanning and Martin Shultz have both recommended
> the following idiom for establishing overridable defaults
>
> pro my_plot, COLOR=color, _EXTRA=extra
> if n_elements(color) eq 0 then color = 12
> plot, COLOR=12, _EXTRA=extra
**** Ooops ^^ ****
> end
>
> This has the effect, unintended and normally irrelevant, that if the
> following call is made with the COLOR keyword set to an undefined variable
>
> my_plot, COLOR=color
>
> then this variable is set to 12 on output. It isn't too hard to imagine a
> situation (successive calls to different routines with different default
> colours) where this will bite an unwary programmer, though in several years
> of using this idiom I have seldom thought about this side-effect and have
> very seldom been bitten.
I have had a difficult time keeping up with this thread. Whew! I
often do my keyword passing with the following draconian but safe
technique.
pro my_plot, COLOR=color0, _EXTRA=extra
if n_elements(color0) eq 0 then color = 12 $
else color = floor(color0(0))
plot, COLOR=color, _EXTRA=extra
end
COLOR0 is the value passed in, which is distinct from the value of the
local variable COLOR. I agree. It's ugly.
Craig
--
--------------------------------------------------------------------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
--------------------------------------------------------------------------