&Defines - &why &doesn't &this &work

Posted by Tim Kuehn on 01-Jul-2014 11:08

I'm doing some work with &define's, and I've got a scenario which I think _should_ work but doesn't. 

This is the code: 

&scoped-define  test    "integer"

&if defined(test)  &then
message "hello1".

&if {&test} = "integer" &then
message "hello2".

/* this works */ 
&if defined(test) &then
&if {&test} = "integer" &then
message "hello3".

/* this doesn't - shouldn't it? */

&if defined(test) and  {&test} = "integer" &then
message "hello3".

All Replies

Posted by Håvard Danielsen on 01-Jul-2014 12:12

defined() returns an integer and should be used as such.


if defined(test) <> 0 &then  

I do not really know why your code works when this is the only expression, but it is probably not doing what you expect or want.  

Posted by Tim Kuehn on 01-Jul-2014 14:06

That did it - thx!

Posted by Stefan Drissen on 02-Jul-2014 01:07

DEFINED returns 0 (not defined), 1 (scoped) or 2 (global). &IF DEFINED (foo) &THEN without a comparison returns false if the value of DEFINED is 0, otherwise true - this short cut does shortly bite you if you want to add operators to the condition and you will need to add the > 0 (I dislike <> when > is sufficient).

Depending on your actual case it can help to /not/ put the quotes around the defined value since you can then use it in more places - for example in the definition of a variable.

&SCOPED-DEFINE test integer

&IF "{&test}" = "integer" &THEN



&SCOPED-DEFINE datatype integer

&IF DEFINED( datatype ) > 0 &THEN

  DEFINE VARIABLE foo AS {&datatype} NO-UNDO.

  MESSAGE foo "{&datatype}".


If you are only using the values as strings then definitely keep the quotes as you will trap typing errors in the preprocessor name with compile errors.

This thread is closed