Jump to content
Ian Branch

{$IFNDEF for Defined(????

Recommended Posts

HI Team,

Hope you all had an enjoyable Xmas day as befits your Faith & Family.

I know and have used

{$IF Defined(xxxx) or Defined(YYYYY)}
.....
.....
{$ENDIF}

Is there a {$IFN..  hidden in Delphi somewhere?? i.e....

{$IFN Defined(XXXXXX) or Defeined(YYYYY)}
.....
.....
{$ENDIF}

I have multiple projects sharing common files and in some case it would be shorter to write {$IFN.... conditionals then {$IF.... conditionals.

 

Regards & TIA,

Ian

Share this post


Link to post
Guest

try this:

implementation

{$R *.dfm}

{$DEFINE HELLO} // change to: HELLOZ  ... later: HELLOW

procedure TForm1.Button1Click(Sender: TObject);
begin
{$IFNDEF HELLO || DEFINED(HELLOW)}  // || == OR  --- left TO right as booelans 
  ShowMessage('HELLOW')
{$ELSE}
  ShowMessage('ALTERNATIVE 2');
{$ENDIF}

end;

 

hug

Edited by Guest

Share this post


Link to post

Thank you gentlemen for both your solutions.

Much appreciated.

FPiette - I didn't realize a NOT could be applied that way.

emailx45 - Sneaky, and another thing I have learnt.

 

A day where nothing is learnt is a day wasted...

 

Regards & All the best for 2021.

 

Ian

Share this post


Link to post
Guest

Editing:

This works too!

//
// because is "one" conditional:   if not defined "HELLO" OR  "HELLOW" but the HELLOW is not considered, because that will be NotDefined HELLO anyway
//
{$IFNDEF HELLO || HELLOW}  // || == OR  --- left TO right as booelans
  • better the sample above! it's more clear!
  • anyway, to samples always analized by: LEFT to right --- who occurrs firstly! ---
  • see your IDE options in Project-Options to change the boolean analize: Complete Boolean Evaluation -- default is FALSE! --- who occurrs firstly! ---

hug

Edited by Guest

Share this post


Link to post

OK.  So I have missed some nuance...

I have the following test code with Users defined...

{$IFDEF  Users}
ShowMessage('{$IFDEF...');
{$ENDIF}
  //
{$IFNDEF  Hello || Bellow || Yellow || Users}
ShowMessage('{$IFNDEF...');
{$ENDIF}

And I have Complete Boolean Evaluation set to True.

Yet I get both messages...

 

This works..

{$IFDEF  Users}
ShowMessage('{$IFDEF...');
{$ENDIF}
  //
{$IFDEF not(defined(Hello)) or not(defined(Bellow)) or not(defined(Yellow)) or not(defined(Users))}
ShowMessage('{$IFNDEF...');
{$ENDIF}

Although my mind says it should be Hello not defined AND Bellow not defined AND Yellow not defined AND Users not defined.

i.e. none of the defines are defined...

 

If I now make it...

  //
{$IFDEF  Users}
ShowMessage('{$IFDEF...');
{$ENDIF}
  //
{$IFDEF not(defined(Hello)) and not(defined(Bellow)) and not(defined(Yellow)) and not(defined(Users))}
ShowMessage('{$IFNDEF...');
{$ENDIF}
  //

And I have Complete Boolean Evaluation set to False.

 

On that basis, I should get both messages with..

  //
{$IFDEF  Users}
ShowMessage('{$IFDEF...');
{$ENDIF}
  //
{$IFDEF not(defined(Hello)) and not(defined(Bellow)) and not(defined(Yellow))}
ShowMessage('{$IFNDEF...');
{$ENDIF}
  //

But no, only the first one.

Interesting stuff.

Edited by Ian Branch

Share this post


Link to post
1 hour ago, Ian Branch said:

{$IFDEF not(defined(Hello)) and not(defined(Bellow)) and not(defined(Yellow))} ShowMessage('{$IFNDEF...'); {$ENDIF}

That is wrong. You used IFDEF instead of IF.

The correct version is:

{$IF not(defined(Hello)) and not(defined(Bellow)) and not(defined(Yellow))}
ShowMessage('{$IFNDEF...');
{$ENDIF}

{]IFDEF XXXX}

if the same as

{$IF Defined(XXX)}

The first form DO NOT ACCEPT boolean expression. The second accept boolean expressions inside which you have the pseudo-function Defined() and you can use Delphi boolean operators.

 

Share this post


Link to post
Guest

The thing is complex, for that need undestand and test it

  • other thing, CONDITIONALs for compilation, dont should be used for any case! like define a value of a "string" etc...
  • just for compilation necessity!
procedure TForm1.FormCreate(Sender: TObject);
begin
  {$B+} // be "- OR +" on Complete Boolean Evaluation ON --- "OR" between conditionals can validate any one!
  // // using "AND" operator, it means "one AND other"  - not "one OR other" -- it should have 2 values
  // // using "OR" operator, it means "one OR other", or be, any one should exist in the conditional!
  //
  { ...$IF DEFINED(ValueA) OR DEFINED(ValueB) OR DEFINED(ValueC) OR DEFINED(ValueD) }
  //
  { ..$DEFINE ValueA }
  { ..$DEFINE ValueB }
  {$DEFINE ValueC }  // here, B and C = ok, else = not ok --- BUT if A or D is defined, then, will be OK! (because of "OR")
  { ..$DEFINE ValueD }
  {$IF DEFINED(ValueA) OR DEFINED(ValueB) AND DEFINED(ValueC) OR DEFINED(ValueD) } // it should exist: ((A or (B AND C)) or D) = A or (B and C) or D
  ShowMessage('Value A to D')  // ok
  {$ELSE}
  ShowMessage('Other values'); // not ok
  {$ENDIF}
  //
  {$B-} // Complete Boolean Evaluation OFF
  //
end;

hug

Edited by Guest

Share this post


Link to post
12 minutes ago, emailx45 said:

The thing is complex

Sorry but YOU made it complex.

 

13 minutes ago, emailx45 said:

{$B+} // be "- OR +" on Complete Boolean Evaluation ON --- "OR" between conditionals can validate any one! // // using "AND" operator, it means "one AND other" - not "one OR other" -- it should have 2 values // // using "OR" operator, it means "one OR other", or be, any one should exist in the conditional!

 

You are completely wrong about complete boolean evaluation. It makes NO DIFFERENCE at all on the result of a conditional expression. When complete evaluation is off, the compiler generate code which do not execute more parts of the expression when the result is already known. This result in faster code at runtime.

 

It makes a difference in code execution when the boolean expression include function call which has side effect. Function may not be called and then side effect not done.

 

Aplyed to conditional compilation, this has no effect since only constant are involved and the expression is resolved at compile time. I'm not even sure that the compiler look at the option when compiling an expression for conditional compilation.

 

Share this post


Link to post

Yup.  My error was in the {$IFDEF.... rather than having {$IF.....

This is all good now. 

  //
{$IF not(defined(Hello)) and not(defined(Bellow)) and not(defined(Yellow))}
ShowMessage('{$IF...');
{$ENDIF}
  //
{$IF not(defined(Hello)) and not(defined(Bellow)) and not(defined(Yellow)) and not(defined(Users))}
ShowMessage('{$IF...');
{$ENDIF}
  //

Tks for the pointers, this works as expected now.  The first shows, the second doesn't.

 

Regards,

Ian

Share this post


Link to post
Guest
5 minutes ago, FPiette said:

You are completely wrong about complete boolean evaluation.

I know evaluation defined as "Short Circuit", where, finding the true value (whether it is false or true) - according to the condition, the rest of the expression will be disregarded, because the "true" point has already been found, in an analysis boolean {$ B-}
And I'm not wrong, the code produced by the constant was already produced at compile time, so the rest is neglected.
Just test my last example by defining the values of A, B, C, and D and see the result that is consistent with my statement.
Otherwise, demonstrate your dispute in code.

Share this post


Link to post
Guest

I don 

13 minutes ago, FPiette said:

Sorry but YOU made it complex.

I dont see none code of your "dont-complex-brain"

Edited by Guest

Share this post


Link to post
Guest

good night, it's 04hrs for here, and i need sleep a little...

see you later  :classic_cheerleader:

Share this post


Link to post
9 minutes ago, emailx45 said:

I dont see none code of your "dont-complex-brain"

I gave the correct code, a single line, before any of your (complex and partially wrong) answers. I answered everything the OP asked. Sorry but the rest is noise in this context.

10 minutes ago, emailx45 said:

good night, it's 04hrs for here, and i need sleep a little...

Have a good night. Here is it already 8h AM.

Share this post


Link to post

In addition to things above, don't forget you can easily add "shortcut defines" just like temporary variables and use them instead of several IF's

{$IF defined(cpu32) and defined(MSWindows) and not defined(Purepascal)}
{$DEFINE UseAsm}
{$IFEND}

...
$ifdef UseAsm ...

 

Edited by Fr0sT.Brutal

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×