Are conditional expressions broken inside packages?

Consider the following snippet:

requires designide, rtl, vcl, {$IF RTLVersion < 19.0} // E2026 Constant expression expected //{$IF CompilerVersion = 22.0} // same as above vcljpg; {$ELSE} vclimg; {$IFEND} 

This seems to be completely syntactically correct. However, the compiler suffocates and reports Constant expression expected . What is really going on here?

Technical: Currently only tested on XE (15.0.3953.35171).

Of course, workarounds are also welcome.

+7
source share
3 answers

I found the same problem in the past even with delphi 2007. As a workaround I use the inc file with conditional definitions and then use {$IFDEF} instead of {$IF}

something like that

 {$I MyDefines.INC} requires designide, rtl, vcl, {$IFDEF DELPHI_XE_UP} //the DELPHI_XE_UP is defineed inside of MyDefines.INC uNewlib; {$ELSE} uOldLib; {$ENDIF} 
+12
source
Modules

package differs from program and library modules. They do not contain executable code, and you cannot use units. Therefore, characters such as RTLVersion simply not visible from the package file. Your only option is to use $IFDEF .

+8
source

I am convinced that I just found a reason. Consider the following:

 {$IF not Declared(RTLVersion)} {$MESSAGE WARN 'There is no RTL'} {$IFEND} {$IF not Declared(CompilerVersion)} {$MESSAGE WARN 'nor are compiler intrinsics at all'} {$IFEND} {$IF not Declared(System)} {$MESSAGE ERROR 'Because package not uses System implicitly'} {$IFEND} 

So, it seems that some kind of compiler is behaving correctly, but it gives out a rather misleading (if not erroneous) message stating that the character is not a constant expression, while the corresponding character is really undeclared !

+2
source

All Articles