Recent

Author Topic: Proper use of {$IF}  (Read 1164 times)

Molochnik

  • Jr. Member
  • **
  • Posts: 79
Proper use of {$IF}
« on: August 14, 2024, 04:39:36 am »
I have two modules
1)
Code: Pascal  [Select][+][-]
  1. unit a;
  2. interface
  3. const
  4. UseFeature = 'Yes';
.....
2)
Code: Pascal  [Select][+][-]
  1. unit b;
  2. uses a{$IF UseFeature='Yes'}, c{$IFEND};
  3.  
...

Version 3.2.2 doesn't recognize UseFeature value throughout all unit b.
3.99 recognizes the value of UseFeature in the implementation part of unit b, but still doesn't recognize it in its uses section.
Delphi handles it perfectly - UseFeature is known in the uses section and all over the whole unit b.
Of course if I change $IF to $IFDEF UseFeature and place it in the project options, it will work, but I dont like it - required full rebuild, often conditional parts are marked as erroneous in Delphi (though they are actually not) and for me it's much cleaner and nicer to have conditions directly in the code than in the project options.
What can be done here? Did I miss something?
« Last Edit: August 14, 2024, 04:41:16 am by Molochnik »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11767
  • FPC developer.
Re: Proper use of {$IF}
« Reply #1 on: August 14, 2024, 09:56:43 am »
May be that the uses line is parsed first, and only then the scope is set up (the units loaded)

The construct seems a bit dodgy to me, ad hoc exploitation of specific compiler implementation.

Zvoni

  • Hero Member
  • *****
  • Posts: 2690
Re: Proper use of {$IF}
« Reply #2 on: August 14, 2024, 10:05:28 am »
{$IF Declared(UseFeature)}?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

wp

  • Hero Member
  • *****
  • Posts: 12354
Re: Proper use of {$IF}
« Reply #3 on: August 14, 2024, 10:59:15 am »
I never was successful with the {$IF} directive in the uses clause. What's definitely working is the {$IFDEF} directive: Create a conditional define {$DEFINE UseFeature} in the project or the package, or add it to an inc file which you {$include ...} in all files needing it. Then in unit b you do

Code: Pascal  [Select][+][-]
  1. unit b;
  2. {$INCLUDE defs.inc}  // assuming that "UseFeature" is defined in file "defs.inc"
  3. uses
  4.   a{$IFDEF UseFeature}, b{$ENDIF};
  5. ...

Thaddy

  • Hero Member
  • *****
  • Posts: 15646
  • Censorship about opinions does not belong here.
Re: Proper use of {$IF}
« Reply #4 on: August 14, 2024, 12:53:22 pm »
I never was successful with the {$IF} directive
Can you provide an example?
If I smell bad code it usually is bad code and that includes my own code.

wp

  • Hero Member
  • *****
  • Posts: 12354
Re: Proper use of {$IF}
« Reply #5 on: August 14, 2024, 01:40:03 pm »
I never was successful with the {$IF} directive
Can you provide an example?
I wrote: "I never was successful with the {$IF} directive in the uses clause."

Molochnik

  • Jr. Member
  • **
  • Posts: 79
Re: Proper use of {$IF}
« Reply #6 on: August 14, 2024, 02:32:57 pm »
I never was successful with the {$IF} directive in the uses clause.
Yes I have the same problem, but thought I missed something. As I said before Delphi handles $IF in the uses section properly.

Thaddy

  • Hero Member
  • *****
  • Posts: 15646
  • Censorship about opinions does not belong here.
Re: Proper use of {$IF}
« Reply #7 on: August 14, 2024, 02:52:12 pm »
Confirm this, but strange. Is there any bug report for that?
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5672
  • Compiler Developer
Re: Proper use of {$IF}
« Reply #8 on: August 15, 2024, 07:50:26 pm »
What can be done here? Did I miss something?

This is currently simply not possible. You can't use constants declared in another unit inside preprocessor macros inside the uses-clause due to the way how the compiler loads units. It simply doesn't know these constants until the trailing semicolon of the clause has been parsed.

Molochnik

  • Jr. Member
  • **
  • Posts: 79
Re: Proper use of {$IF}
« Reply #9 on: August 17, 2024, 07:39:14 pm »
This is currently simply not possible. You can't use constants declared in another unit inside preprocessor macros inside the uses-clause due to the way how the compiler loads units. It simply doesn't know these constants until the trailing semicolon of the clause has been parsed.
Ok got it. It's not a big problem, though it actually reduces the $IF value almost to zero. Why use it if $IFDEF works always and everywhere?
« Last Edit: August 18, 2024, 10:03:24 am by Molochnik »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5672
  • Compiler Developer
Re: Proper use of {$IF}
« Reply #10 on: August 18, 2024, 11:36:32 am »
This is currently simply not possible. You can't use constants declared in another unit inside preprocessor macros inside the uses-clause due to the way how the compiler loads units. It simply doesn't know these constants until the trailing semicolon of the clause has been parsed.
Ok got it. It's not a big problem, though it actually reduces the $IF value almost to zero. Why use it if $IFDEF works always and everywhere?

$IFDEF has the same restrictions here, there is no difference between $IF and $IFDEF regarding that. And after the uses-clause you can $IF on identifiers from another unit.

Also the advantage of $IF compared to $IFDEF is that you can combine multiple expressions using OR and AND.

 

TinyPortal © 2005-2018