18

I wish to have the following:

TEnumType = (
  etValue1 = 1,
  etValue2 = 2 deprecated,
  etValue3 = 3);

It returns:

[DCC Error] unt_CollectionImportType.pas(19): E2029 ',' or ')' expected 
but identifier 'deprecated' found.

Is there a way to instruct the compiler that this value is deprecated.

starblue
  • 55,348
  • 14
  • 97
  • 151
Gad D Lord
  • 6,620
  • 12
  • 60
  • 106

1 Answers1

36
type
  TEnumType = (
    etValue1 = 1,
    etDeprecated2 = 2, // was: etValue2; Renamed so we can deprecate it by name
    etValue3 = 3);

const
   etValue2 = etDeprecated2 deprecated; // Declares a constant mapped to the renamed enum value.
Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
Zoë Peterson
  • 13,094
  • 2
  • 44
  • 64
  • Doesn't that just deprecate etValue2, and not etDeprecated2? – Rudy Velthuis Feb 24 '12 at 00:32
  • @Rudy: Sure, but the assumption is that etValue2 used to be declared in the enum and got refactored out to the above. – afrazier Feb 24 '12 at 00:37
  • 7
    @Rudy - yes, but that was the objective. Replace the actual enum value with a name that won't be used, then declare the existing (deprecated) enum value as an 'alias' for the new name and mark that *alias* as deprecated. Existing code is using the old name (the alias) and thus use of the deprecated value name is reported via a warning by the compiler. This answer solves the problem perfectly, and quite cleverly too imho. – Deltics Feb 24 '12 at 00:40
  • @Deltics, afrazier: Oh, OK, that makes sense and is indeed pretty clever. – Rudy Velthuis Feb 24 '12 at 00:42
  • 4
    Having said that, I would have said that if the value shouldn't be used anymore, then simply removing it or changing it's name should be enough. Then instead of compiler warnings you get compiler errors unless and until you have eliminated all use of the deprecated symbol (which is surely the ultimate goal). – Deltics Feb 24 '12 at 00:43
  • 7
    +1, very nice. This is the kind of *answer* that makes me think I should *ask more questions*. – Cosmin Prund Feb 24 '12 at 07:18
  • 2
    @Deltics: that can only be done if the values are given as in the example. When you rely on the implicit enum numbering (ie `TEnumType = (etZero, etOne, Two)`) you cannot/shouldnot remove a value from the list ever, and especially not when you store the corresponding numeric values somewhere else. Renaming is the only option then. I agree that the alias is not needed, but it certainly provides for helpful warning instead of just an error message about an identifier not having been declared. – Marjan Venema Feb 24 '12 at 08:08
  • Accepted as being a possible workaround for a non existing feature. – Gad D Lord Feb 25 '12 at 20:05
  • @Marjan - well, the corollary is of course that you should not rely on implicit enum numbering anyway. By all means *use* implicit numbering, but if you do you should not then be using those in a way that is dependent upon the implicit numbering scheme (besides any changes you may make, the implicit numbering scheme may change in a new version of the compiler... unlikely, but not impossible). – Deltics Feb 27 '12 at 00:19
  • 4
    +1 though a warning is in place: this will break DFM streaming of the deprecated value, as the DFM streaming will look into the enumeration type for the values, not in the const. So when you have an existing DFM file containing the value `etValue2` it will crash when reading that DFM file. – Jeroen Wiert Pluimers Feb 27 '12 at 10:16