Are conditional expressions broken within packages? - delphi

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.

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}

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.

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!

Related

Generics in Delphi and returning a reference to tlist<class>

I still use Delphi XE4 (newest compiler I use of multiple Delphi compilers) and need a specific workaround for the fact they completely hid FClients in TBasicAction in this version. I connect/disconnect clients runtime while setting enabled/disabled (to avoid flicker with ~100+ actions and ui elements) thus this workaround for XE4:
Here's my naive attempt and simply returning the field.
TmscBasicActionCrack = class(TBasicAction)
end;
{$IFDEF mymsDELPHIXE4}
TmscBasicActionHelper = class helper for TBasicAction
public
function Helper_Get_Private_FClients: TList<System.Classes.TBasicActionLink>;
end;
{$ENDIF}
{$IFDEF mymsDELPHIXE4}
//------------------------------------------------------------------------------
function TmscBasicActionHelper.Helper_Get_Private_FClients: TList<System.Classes.TBasicActionLink>;
begin
Result := Self.FClients;
end;
{$ENDIF}
However, I get error
E2003 Undeclared identifier: TList<>
I must admit I never go around to using generics with Delphi since I initially heard of stability problems + I need to maintain compability with Lazarus/FreePascal.
I am aware the most recent versions Delphi has altered class helpers again, but I am for now mostly interested in getting this to work with Delphi XE4
The error is indicating that the TList<T> type is unknown to the compiler. To use it you must include System.Generics.Collections in your uses clause.

Delphi CompilerVersion directive issues

I have a library code file which is shared between Delphi 5 and DelphiXE2. I am attempting to add anonymous method functionality to the code file, but only for DelphiXE2 projects (since Delphi 5 doesn't support anonymous methods). It seemed I should be able to use the CompilerVersion (Note: I don't want to limit it to DelphiXE2, just in case we ever upgrade).
{$IF CompilerVersion >= 23}
{$DEFINE AnonymousAvail}
{$IFEND}
This worked nicely in XE2, but it turns out, Delphi 5 doesn't support the $IF directive. I decided to wrap it in an $IFDEF. This worked nicely in Delphi 5, but XE2 also doesn't seem to have CompilerVersion defined, so AnonymousAvail is not defined.
{$IFDEF CompilerVersion}
{$IF CompilerVersion >= 23}
{$DEFINE AnonymousAvail}
{$IFEND}
{$ENDIF}
Any help would be appreciated.
Note: I cannot move anonymous method code to a different code file.
Do what the documentation says:
{$IFDEF ConditionalExpressions}
{$IF CompilerVersion >= 23.0}
{$DEFINE AnonymousAvailable}
{$IFEND}
{$ENDIF}
Be sure that the outer condition is as shown (and closed with ENDIF) and you can use CompilerVersion and other constants and expressions inside.
You can also use
{$IF defined(BLAH)}
or, one of my favourites:
{$IF declared(AnsiString)}
etc...
FWIW, I noticed that the example in the link comes, almost verbatim, from my Console.pas unit.

Why doesn't code from "The Tomes of Delphi" compile?

I'm trying to use the TDRecLst and TDSplyCm units from the code included with The Tomes of Delphi, but I get a compiler error in TDBasics.pas:
I get a similar error in TDStrRes.inc:
What's wrong, and how do I fix it?
The code is available from the author.
You're evidently using a Delphi version that's newer than Delphi 6. Despite being updated in 2005, the code from that book only detects up to that version of Delphi. TDDefine.inc defines a number of compiler symbols based on the version it detects, but when the version you're using isn't anything it recognizes, it defines no symbols. That eventually leads to problems later when the compiler encounters code like this in TDBasics.pas;
implementation
uses
{$IFDEF Delphi1}
WinTypes, WinProcs;
{$ENDIF}
{$IFDEF Delphi2Plus}
Windows;
{$ENDIF}
{$IFDEF Kylix1Plus}
Types, Libc;
{$ENDIF}
{$IFDEF Delphi1}
{$R TDStrRes.r16}
{$ENDIF}
{$IFDEF Delphi2Plus}
{$R TDStrRes.r32}
{$ENDIF}
{$IFDEF Kylix1Plus}
{$R TDStrRes.r32}
{$ENDIF}
const
UnitName = 'TDBasics';
Since none of Delphi1, Delphi2Plus, or Kylix1Plus is defined, the uses clause is empty. When we ignore all the compiler directives and inactive code blocks, the compiler ultimately sees code like this:
implementation
uses
const
UnitName = 'TDBasics';
That's why the compiler complains about expecting an identifier instead of const.
To fix it, you need to teach TDDefine.inc to recognize your version of Delphi. Easier, though, might be to ignore all the version-detection code and hard-code all the symbols that apply to the version you're using. As long as you never use any version older than Delphi 6, all the symbols will apply to all your versions.
Find the following block of code in TDDefine.pas:
{$IFDEF VER140}
{$DEFINE Delphi6}
{$DEFINE Delphi1Plus}
{$DEFINE Delphi2Plus}
{$DEFINE Delphi3Plus}
{$DEFINE Delphi4Plus}
{$DEFINE Delphi5Plus}
{$DEFINE Delphi6Plus}
{$DEFINE HasAssert}
{$ENDIF}
Remove the first and last lines so that the remaining $DEFINE instructions are processed unconditionally.

How can i detect specific RTL features at compile time?

For sake of example lets check for infamous TStrings.StrictDelimiter:
{$IF Declared(TStrings.StrictDelimiter)}
{$MESSAGE WARN 'Beware of TStrings.StrictDelimiter which is False by default!'}
{$IFEND}
However, Declared compiler intrinsic reports syntax error on conditional line: E2029 ')' expected but '.' found. (tested on XE)
For Delphi XE2 I'm using this :
{$IFDEF BDS9}
Result.VersionString := 'Delphi XE2 ' +
{$IF NOT DECLARED(Consts.SStyleFeatureNotSupported)}
'(original release version)'
{$ELSE} {$IF NOT DECLARED(FireMonkeyVersion)} // D2010 chokes when scope (FMX.Types) is mentioned!
'Update 1'
{$ELSE} {$IF NOT DECLARED(System.TestSSE)}
'Update 2'
{$ELSE}
'Update 3'
// TODO : Update this for any following update!
{$IFEND} {$IFEND} {$IFEND}
;
{$ELSE}
{$IFDEF BDS7}
Result.VersionString := 'Delphi 2010';
{$ELSE}
{$MESSAGE ERROR 'Extend this!'}
{$ENDIF}
{$ENDIF}
In other words : I test for the existence of symbols that are introduced since any particular delphi-version. The same construct can be used to set a variable or constant or compiler define, so further code can use these instead.
Note : I keep a backup around of the Source folder for all Delphi versions that I've had installed in the past few years. Putting these folders through a tool like BeyondCompare and browsing through the differencing files, will quickly give you a few symbols that you can test for....
Unfortunately this kind of expressions aren't supported, you have to know in which RTL / compiler version some feature was introduced and then use predefined conditional symbols like VER<nnn>, RTLVersion, CompilerVersion etc.

Can I generate a custom compiler error? If so, how?

Here is what I want to do. I have a project that must be compiled in some version of Delphi or later. I would like to use a conditional compiler directive to test the Delphi version, and then cause a custom compiler error to be generated with a custom message. Being able to generate a custom compiler warning or hint would also be adaquate if an error is not possible.
Sure, I could put some un-compilable giberish in the conditional code segment, and that's fine. But my question is "Can I generate, conditionally, a custom compiler error?"
Thank you Johan and Serg.
Here is the solution, and more details about the issue. I have an application that was originally built in Delphi 2007. It includes Internet Direct components to attach to a Web service. These use SSL. I recently upgraded my SSL libraries to a later version, and these don't play so well with the Delphi 2007 Indy components. I have now added the following compiler directives to ensure that this application will no longer be compiled with Delphi 2007 or earlier:
{$IF CompilerVersion <= 19.0} // Delphi 2007 = 19.0
{$MESSAGE Error 'This project must be compiled in Delphi 2009 or later'}
{$IFEND}
You can use:
{$Message HINT|WARN|ERROR|FATAL 'text string' }
{$MESSAGE 'Boo!'} emits a hint
{$Message Hint 'Feed the cats'} emits a hint
{$messaGe Warn 'Looks like rain.'} emits a warning
{$Message Error 'Not implemented'} emits an error, continues compiling
{$Message Fatal 'Bang. Yer dead.'} emits an error, terminates compiler
See: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsmessagedirective_xml.html
This works in Delphi 6 and later.
Checking the Delphi version has become easy since CONDITIONALEXPRESSIONS directive was introduced in Delphi 6:
program requires2010;
{$APPTYPE CONSOLE}
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF CompilerVersion >= 21.0} // 21.0 is Delphi 2010
{$DEFINE DELPHI2010}
{$IFEND}
{$ENDIF}
begin
{$IFNDEF DELPHI2010}
{$MESSAGE Fatal 'Wrong Delphi Version'}
{$ENDIF}
Writeln('Continued');
Readln;
end.

Resources