I see your class helper and raise you mine!

Small tip: In one of my old posts, I ranted about how Embarcadero changed enumerated types in FMX to drop the prefix around XE6 and added a ton of deprecated warning messages through class helpers, i.e., instead of this:

TAlignLayout = (alNone, alTop, alLeft, alRight, alBottom, alMostTop, alMostBottom, alMostLeft, alMostRight, alClient,alContents, alCenter, alVertCenter, alHorzCenter, alHorizontal, alVertical, alScale, alFit, alFitLeft, alFitRight);

it became this:

 TAlignLayout = (None, Top, Left, Right, Bottom, MostTop, MostBottom, MostLeft, MostRight, Client, Contents, Center, VertCenter, HorzCenter, Horizontal, Vertical, Scale, Fit, FitLeft, FitRight);

 TAlignLayoutHelper = record helper for TAlignLayout
 const
 alNone = TAlignLayout.None deprecated 'Use TAlignLayout.None';
 alTop = TAlignLayout.Top deprecated 'Use TAlignLayout.Top';
 alLeft = TAlignLayout.Left deprecated 'Use TAlignLayout.Left';
 alRight = TAlignLayout.Right deprecated 'Use TAlignLayout.Right';
 alBottom = TAlignLayout.Bottom deprecated 'Use TAlignLayout.Bottom';
 alMostTop = TAlignLayout.MostTop deprecated 'Use TAlignLayout.MostTop';
 alMostBottom = TAlignLayout.MostBottom deprecated 'Use TAlignLayout.MostBottom';
 alMostLeft = TAlignLayout.MostLeft deprecated 'Use TAlignLayout.MostLeft';
 alMostRight = TAlignLayout.MostRight deprecated 'Use TAlignLayout.MostRight';
 alClient = TAlignLayout.Client deprecated 'Use TAlignLayout.Client';
 alContents = TAlignLayout.Contents deprecated 'Use TAlignLayout.Contents';
 alCenter = TAlignLayout.Center deprecated 'Use TAlignLayout.Center';
 alVertCenter = TAlignLayout.VertCenter deprecated 'Use TAlignLayout.VertCenter';
 alHorzCenter = TAlignLayout.HorzCenter deprecated 'Use TAlignLayout.HorzCenter';
 alHorizontal = TAlignLayout.Horizontal deprecated 'Use TAlignLayout.Horizontal';
 alVertical = TAlignLayout.Vertical deprecated 'Use TAlignLayout.Vertical';
 alScale = TAlignLayout.Scale deprecated 'Use TAlignLayout.Scale';
 alFit = TAlignLayout.Fit deprecated 'Use TAlignLayout.Fit';
 alFitLeft = TAlignLayout.FitLeft deprecated 'Use TAlignLayout.FitLeft';
 alFitRight = TAlignLayout.FitRight deprecated 'Use TAlignLayout.FitRight';
 end;

This was a dumb idea for multiple reasons.  The worse one being that loading a FMX form in later versions of Delphi would break the form for earlier versions (as alClient would be replaced by Client and then be an unknown value in pre-XE6 versions).  However, nearly as annoying is that when you write code that support multiple versions of Delphi, all of a sudden you would get gratuitous deprecation warnings in later versions of Delphi.  It is really annoying to sprinkle IFDEFs all over your code for small changes like this, just to get rid of some warnings.

In one of those slap-your-hand-against-your-forehead-why-didn’t-I-think-of-this-earlier moments, I finally realized a few weeks ago that I could use a limitation of record/class helpers (there can only be one) to override their record/class helper in my code with my new and improved class helper that gets rid of the deprecations:

 TMyAlignLayoutHelper = record helper for TAlignLayout
 const
 alNone = TAlignLayout.None;
 alTop = TAlignLayout.Top;
 alLeft = TAlignLayout.Left;
 alRight = TAlignLayout.Right;
 alBottom = TAlignLayout.Bottom;
 alMostTop = TAlignLayout.MostTop;
 alMostBottom = TAlignLayout.MostBottom;
 alMostLeft = TAlignLayout.MostLeft;
 alMostRight = TAlignLayout.MostRight;
 alClient = TAlignLayout.Client;
 alContents = TAlignLayout.Contents;
 alCenter = TAlignLayout.Center;
 alVertCenter = TAlignLayout.VertCenter;
 alHorzCenter = TAlignLayout.HorzCenter;
 alHorizontal = TAlignLayout.Horizontal;
 alVertical = TAlignLayout.Vertical;
 alScale = TAlignLayout.Scale;
 alFit = TAlignLayout.Fit;
 alFitLeft = TAlignLayout.FitLeft;
 alFitRight = TAlignLayout.FitRight;
 end;

Voila, no more deprecation warnings in my code and no need for lots of IFDEFs (the forms problem is another matter).  I am passing along this tip for those who need it.  I know that it cleaned up my code a great deal.

Now I am sure that someone will be upset that I am subverting their deprecation warnings and that they must be there for some good reason.  My response… ah, no, they are not.  It was stupid.  And even though Embarcadero may drop their deprecated enumerated values someday, my record helper will be there to add the functionality right back in; no need to ever deprecate a constant.  Of course, this is my opinion and you are welcome to yours.  For me, I am tempted to submit Embarcadero’s code to Parnassus’ Bad Delphi Code Competition 🙂

Happy CodeSmithing!

Leave a Reply

Your email address will not be published. Required fields are marked *