Are conditional expressions broken within packages?
O

3

7

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}

It seems to be absolutely syntactically correct. However, the compiler chokes on it and reports Constant expression expected. What really happens here?

Technical: currently tested on XE (15.0.3953.35171) only.

Of course, workaround suggestions are welcome too.

Ostrogoth answered 29/11, 2011 at 3:58 Comment(2)
FWIW: In D2007 the {$IF RTLVersion < 19.0} test works if used after the containsclause.Hibernicism
@Ulrich Gerhardt, quite interesting observation, thanks! (confirmed with XE)Ostrogoth
M
12

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

something like so

{$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}
Minorca answered 29/11, 2011 at 4:25 Comment(2)
Which defines.inc you'd recommend to reuse (amongst maintained and w/o viral license)?Ostrogoth
Try using the inc files included in the jedi project (jcl or jvcl)Minorca
J
8

package modules are different from program and library modules. They do not contain executable code and you cannot use units. Therefore, symbols like RTLVersion are simply not visible from a package file. Your only option is to use $IFDEF.

Julius answered 29/11, 2011 at 9:37 Comment(2)
And it is not exactly correct, see Ulrich Gerhardt's comment to OP.Ostrogoth
Well, I didn't know that but it's fairly useless sadly since the only thing that comes after contains is end..Julius
O
2

I'm convinced what i just found the cause. 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 appears to be what compiler behaves correctly, but issues a rather misleading (if not erroneous) message about symbol not being a constant expression, while symbol in question actually is undeclared!

Ostrogoth answered 29/11, 2011 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.