Undefined variables in structures [message #62437] |
Wed, 10 September 2008 02:16  |
Joost Aan de Brugh
Messages: 16 Registered: July 2008
|
Junior Member |
|
|
Hello all,
What is the most plausible way to undefine variables in a structure.
Sometimes, you need undefined variables to put into optional arguments
if the procedure checks with N_Elements(..) ne 0.
For example:
plot,....,color=fsc_color(colorname,filename=consts.COLOR_FI LENAME),...
If I do not want to use an own color file, I need to undefine
consts.COLOR_FILENAME. Note that "" as filename is an error.
Several attempts
1
if consts.COLOR_FILENAME eq "" then void =
Temporary(consts.COLOR_FILENAME)
; does not work in a struct. consts.COLOR_FILENAME remains ""
2
temp_color_filename = consts.COLOR_FILENAME
if consts.COLOR_FILENAME eq "" then void =
Temporary(temp_color_filename)
plot,...,color=fsc_color(color,filename=temp_color_filename) ,...
; Not the way to go. Makes the code messy, especially when more
variables have to be undefined this way
3
if consts.COLOR_FILENAME eq "" then
plot,...,color=fsc_color(color),... else
plot,...,color=fsc_color(color,filename=consts.COLOR_FILENAM E)
; Not the way to go. Will create a huge if-then-else pyramid when
using several optional arguments.
4
plot,...,color=fsc_color(color,filename=consts.COLOR_FILENAM E eq ""?
undefined:consts.COLOR_FILENAME),...
; No pyramid because of the ? : construction But this is very hacky,
because I am using a nonsense name (undefined).
5 ; The way I now just thought of.
Make it a pointer and during startup
if *const.COLOR_FILENAME eq "" then void =
Temporary(*consts.COLOR_FILENAME)
and then simply
plot,...,color=fsc_color(color,filename=*consts.COLOR_FILENA ME)
; This does work. I can dereference the pointer, because it is a valid
pointer to an undefined heap variable. It is still a little bit
tricky, but the least evil of these possibilities (in my opinion).
Are there people with more experience with these kinds of
constructions and know a better way to do this.
(Actually, I was already formulation this question before I thought
about the pointer)
|
|
|
Re: Undefined variables in structures [message #62506 is a reply to message #62437] |
Thu, 11 September 2008 08:28  |
Jeremy Bailin
Messages: 618 Registered: April 2008
|
Senior Member |
|
|
On Sep 10, 5:16 am, Joost Aan de Brugh <joost...@gmail.com> wrote:
> Hello all,
>
> What is the most plausible way to undefine variables in a structure.
> Sometimes, you need undefined variables to put into optional arguments
> if the procedure checks with N_Elements(..) ne 0.
>
> For example:
> plot,....,color=fsc_color(colorname,filename=consts.COLOR_FI LENAME),...
> If I do not want to use an own color file, I need to undefine
> consts.COLOR_FILENAME. Note that "" as filename is an error.
>
> Several attempts
> 1
> if consts.COLOR_FILENAME eq "" then void =
> Temporary(consts.COLOR_FILENAME)
> ; does not work in a struct. consts.COLOR_FILENAME remains ""
>
> 2
> temp_color_filename = consts.COLOR_FILENAME
> if consts.COLOR_FILENAME eq "" then void =
> Temporary(temp_color_filename)
> plot,...,color=fsc_color(color,filename=temp_color_filename) ,...
> ; Not the way to go. Makes the code messy, especially when more
> variables have to be undefined this way
>
> 3
> if consts.COLOR_FILENAME eq "" then
> plot,...,color=fsc_color(color),... else
> plot,...,color=fsc_color(color,filename=consts.COLOR_FILENAM E)
> ; Not the way to go. Will create a huge if-then-else pyramid when
> using several optional arguments.
>
> 4
> plot,...,color=fsc_color(color,filename=consts.COLOR_FILENAM E eq ""?
> undefined:consts.COLOR_FILENAME),...
> ; No pyramid because of the ? : construction But this is very hacky,
> because I am using a nonsense name (undefined).
>
> 5 ; The way I now just thought of.
> Make it a pointer and during startup
> if *const.COLOR_FILENAME eq "" then void =
> Temporary(*consts.COLOR_FILENAME)
> and then simply
> plot,...,color=fsc_color(color,filename=*consts.COLOR_FILENA ME)
> ; This does work. I can dereference the pointer, because it is a valid
> pointer to an undefined heap variable. It is still a little bit
> tricky, but the least evil of these possibilities (in my opinion).
>
> Are there people with more experience with these kinds of
> constructions and know a better way to do this.
> (Actually, I was already formulation this question before I thought
> about the pointer)
For just one variable, option 5 is probably the way to go. If you'll
have lots of possible options that you give one function or procedure,
though, you might want to consider using AUGMENT_INHERITED_KEYWORD in
JBIU:
http://web.astroconst.org/jbiu/jbiu-doc/misc/augment_inherit ed_keyword.html
-Jeremy.
|
|
|