Following up the discussion about conditional compilation that evolved from David Millington’s post on Google+ – as I mentioned there in my comments (and this answer on stackoverflow) I suggest using conditional compilation based on {$IF-constructs checking the built in constants CompilerVersion and RTLVersion. C’mon, this is available since Delphi 6 (nearly 15 years now)! It makes life so much easier.

If we only could have named constants for each Delphi version so one doesn’t have to remember which version has which number. A common complaint regarding this feature.

Here is my answer to it:

System.Defines.pas unit System.Defines; interface const RTLVersionSeattle = 30.0; RTLVersionXE8 = 29.0; RTLVersionXE7 = 28.0; RTLVersionXE6 = 27.0; RTLVersionXE5 = 26.0; RTLVersionXE4 = 25.0; RTLVersionXE3 = 24.0; RTLVersionXE2 = 23.0; RTLVersionXE = 22.0; RTLVersion2010 = 21.0; RTLVersion2009 = 20.0; RTLVersion2007Net = 19.0; RTLVersion2007 = 18.5; RTLVersion2006 = 18.0; RTLVersion2005 = 17.0; RTLVersion8Net = 16.0; RTLVersion7 = 15.0; const CompilerVersionSeattle = 30.0; CompilerVersionXE8 = 29.0; CompilerVersionXE7 = 28.0; CompilerVersionXE6 = 27.0; CompilerVersionXE5 = 26.0; CompilerVersionXE4 = 25.0; CompilerVersionXE3 = 24.0; CompilerVersionXE2 = 23.0; CompilerVersionXE = 22.0; CompilerVersion2010 = 21.0; CompilerVersion2009 = 20.0; CompilerVersion2007 = 18.5; CompilerVersion2006 = 18.0; CompilerVersion2005 = 17.0; CompilerVersion8Net = 16.0; CompilerVersion7 = 15.0; implementation end. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 unit System . Defines ; interface const RTLVersionSeattle = 30.0 ; RTLVersionXE8 = 29.0 ; RTLVersionXE7 = 28.0 ; RTLVersionXE6 = 27.0 ; RTLVersionXE5 = 26.0 ; RTLVersionXE4 = 25.0 ; RTLVersionXE3 = 24.0 ; RTLVersionXE2 = 23.0 ; RTLVersionXE = 22.0 ; RTLVersion2010 = 21.0 ; RTLVersion2009 = 20.0 ; RTLVersion2007Net = 19.0 ; RTLVersion2007 = 18.5 ; RTLVersion2006 = 18.0 ; RTLVersion2005 = 17.0 ; RTLVersion8Net = 16.0 ; RTLVersion7 = 15.0 ; const CompilerVersionSeattle = 30.0 ; CompilerVersionXE8 = 29.0 ; CompilerVersionXE7 = 28.0 ; CompilerVersionXE6 = 27.0 ; CompilerVersionXE5 = 26.0 ; CompilerVersionXE4 = 25.0 ; CompilerVersionXE3 = 24.0 ; CompilerVersionXE2 = 23.0 ; CompilerVersionXE = 22.0 ; CompilerVersion2010 = 21.0 ; CompilerVersion2009 = 20.0 ; CompilerVersion2007 = 18.5 ; CompilerVersion2006 = 18.0 ; CompilerVersion2005 = 17.0 ; CompilerVersion8Net = 16.0 ; CompilerVersion7 = 15.0 ; implementation end .

Update: Fixed wrong CompilerVersion values. Thanks to Stefan Glienke for point this out.

Want to check for string helpers (introduced in XE3)?

{$IF RTLVersion < RTLVersionXE3} result := Uppercase(S); {$ELSE} result := S.ToUpper; {$ENDIF} 1 2 3 4 5 { $ IF RTLVersion < RTLVersionXE3 } result : = Uppercase ( S ) ; { $ ELSE } result : = S . ToUpper ; { $ ENDIF }

I would love to see these constants baked into the next System.pas.

Update: As Oliver correctly mentioned in his comment on my Google+ post, this won’t help anyway because it will only affect the newest version. Checking for “less than the current version” simply won’t work in older versions as those cannot gain knowledge of that now current version after they are released. So the above example simply would not work in XE2 (assuming those constants would have been available then) just because something like RTLVersionXE3 was probably not declared at that time. (The problem are not the numbers but the names.) As he also pointed out, a better solution would be to have those declarations publicly available and officially managed by Embarcadero.

Side note: I was a bit surprised to find checking for CompilerVersion inside the delivered Delphi sources. Not a bad move, IMHO.