Refactoring for enumerated types

Posted by on in Blogs

Few day ago I faced with the problem. Many enumerated types declared like this:

TMyType = (mtOne, mtTwo, mtThree, mtFour, mtFive);

It was comfortable in older versions of Delphi, when we assigned a value without specifying the type. Ie

LVariable : TMyType;
LVariable := mtThree;


Times have changed now specify the type and prefixes no need anymore.

  LVariable := TMyType.mtThree;

How to remove a prefix most painless? Of course, we can create a new kind of type, the old type mark as Deprecated, add a bunch of methods and shoveled a lot of code, because the need to maintain compatibility with legacy code.
There is a solution and it's easier solution suggested by Allen Bauer - use helpers.

TMyType = (One, Two, Three, Four, Five);

TMyTypeHelper = record helper for TMyType
mtOne = TMyType.One deprecated 'Use TMyType.One';
mtTwo = TMyType.Two deprecated 'Use TMyType.Two';
mtThree = TMyType.Three deprecated 'Use TMyType.Three';
mtFour = TMyType.Four deprecated 'Use TMyType.Four';
mtFive = TMyType.Five deprecated 'Use TMyType.Five';

And now works as the old code and the new code. Only for each use of the old prefixes we get a warning

[dcc32 Warning] Unit41.pas(47): W1000 Symbol 'eThree' is deprecated: 'Use TMyType.Three'
Tags: eng


  • Guest
    nikolay Monday, 25 November 2013

    In case when our customers use this enumeration we can't just delete it.
    For example: we have some component with enumeration, we want change name for some enumeration but our customers already use it.
    Use this solution we can add new name and mark old name as deprecated.
    We will retain compatibility with old code and will be able to use the new name.

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: