Is possible to mark an entire const block as deprecated?
Asked Answered
G

4

12

I know you can mark a single constant as deprecated using

const
  NotDeprConst1 = 1;
  DeprConst = 2 deprecated;
  NotDeprConst2 = 2;

But, can you mark a whole const block as deprecated without marking the constants one by one?

I would like to do something like:

const deprecated
  DeprConst1 = 1;
  DeprConst2 = 2;
  DeprConst3 = 3;

That doesn't compile however (the compiler sees "deprecated" as a identifier).

Or maybe there's a compiler directive:

{$DEPRECATED ON}
const
  DeprConst1 = 1;
  DeprConst2 = 2;
  DeprConst3 = 3;
{$DEPRECATED OFF}

Embarcadero's hinting directives documentation says you can mark any declaration with a hint (like deprecated) but doesn't elaborate.

Gallaher answered 4/12, 2014 at 14:7 Comment(16)
It seems to me you could try this and see what happens in less time than it took for you to post the question here and format it and create the link. Why didn't you?Hilmahilt
Try this? you mean "const deprecated"? That doesn't compile. I'll update the question.Gallaher
He could try that one specific idea, @Ken, but that doesn't rule out some other syntax for accomplishing the goal that Daniel didn't think of. Your answer addresses the example, but doesn't really answer the question.Honky
@Rob: The specific question says "can you mark a whole const block as deprecated without marking the constants one by one?", which is specifically what I answered (as it was worded at the time I posted my answer). How does it not answer the question?Hilmahilt
@Ken, Daniel asked broadly about how to deprecate a whole block. He suggested one possible solution. You showed that the solution doesn't work, but didn't say whether it's possible to deprecate a block any other way. Daniel later demonstrated the point by adding another suggested solution. I can think of a few more. We can brainstorm syntaxes and you can test them for us all day, but it still won't answer the question of whether it's possible to do in the first place (unless we happen to guess one that works after all).Honky
@Rob: Read the subject of the question ("entire const block") and the sentence I previously quoted ("whole const block"); there is nothing "broad" about it. If Daniel actually had in mind a different question, he should have asked that different question. The general practice here is to answer the specific question that was asked, which I did; your statement that I didn't do so is incorrect. (Also see the revision history for the original state of the question at the time I answered it.)Hilmahilt
@KenWhite I understand the question as "Is it possible to...", but you seem to interpret it as "Will this code work..."Paulin
@Jerry: No, I understand the question as what you see in revision #2 in the history, which is the question I answered.Hilmahilt
const deprecated N = 42; violates pascal grammar rules. Documentation is pretty clear about what you mark a declaration with hint directive. When you declare three constants you have to use directive on each declaration. There is no real solution for your problem. As yet another semi-solution you could convert constants to enumerated type and deprecate that type.Charmaincharmaine
I just checked the revision history, and it contains "Something like:" starting from #1. For me this means the OP is also looking for alternatives.Skim
@UliGerhardt I don't even see it as "also" - OP has obviously attempted that code already, but to no avail, and thus sought help on Stack Overflow :-) So funny how quickly people jump to conclusions. I find myself doing so often as well.Paulin
It's a shame that the accepted answer here says anything other than "It cannot be done, deprecate each item individually".Nanny
@DavidHeffernan It says you can do it by moving the constants inside an unit and deprecating the whole unit. I don't see where's the part about deprecating each one.Gallaher
Yes, I can see that. That's not my point. My point is that moving stuff lock stock to a deprecated unit entirely defeats the purpose of deprecation.Nanny
@David Not quite. The effort of deciding what to do with each use of one of the deprecated constants would be more than adding a new deprecated unit to the uses clause of each unit that uses them. However that said, I still think OP's best bet is to just use the IDE's macro recorder to quickly depecrate the consts individually.Impi
Indeed, I have used the Macros very widely for very similar reasons, don't know where I'd be without it. I've also used MS Excel with a little column/row trickery to manipulate massive SQL statements, but that's another story :-)Paulin
B
13

As you have found out, a const block can not be deprecated in one go. There's also no compiler directive like you were speculating. However, the documentation you refer to says

When a hint directive appears in a unit declaration, it means that the hint applies to everything in the unit. For example, the Windows 3.1 style OleAuto.pas unit on Windows is completely deprecated. Any reference to that unit or any symbol in that unit produces a deprecation message.

By moving deprecated const declarations to a new unit and marking that unit deprecated, you can deprecate a larger amount of declarations in one go. Then, of course, you still need to fix unit references. Whether it saves effort or not is for you to decide.

Bors answered 4/12, 2014 at 15:19 Comment(7)
I was hoping to avoid moving stuff aroundGallaher
@Daniel I can understand that, but there doesn't seem to be any other way than a)individually marking, b) moving to other unit or c) Teuns suggestion, which requires lot of editing. Are we talking about tens of items or hundreds?Bors
Around 1050 constants in a text file used (via $include) in more than 110 units spread among 15 different projectsGallaher
@Daniel That puts your original question in a totally different perspective from what I understood when I first read it. Maybe a editor macro could do individual marking for you?Bors
So, what you're saying is that you have to edit 1050 items in a single file. The number of files or projects it's used in doesn't make a difference; the compiler takes care of that duplication automatically.Honky
@Daniel, FWIW: The editing of the 1050 constants doesn't need to take a long time. With a bit of regex magic it should done in a few minutes.Skim
@Daniel The Delphi IDE supports keyboard macros. Ctrl+Shift+R to start/stop recording a macro. And Ctrl+Shift+P to replay the macro.Impi
T
3

It is possible but it will take some work and simply marking them all as deprecated will be much much easier.

Anyhow here is how you can do it:

Old situation

type
  TMyClass = Class
  private  
  public  
    const
      Const1 = 1;
      Const2 = 2;
      Const3 = 3;
  end;

New situation

type
  TDeprecatedClass = Class
  private
  public
    const
      Const1 = 1;
      Const2 = 2;
      Const3 = 3;
  end deprecated;

  TMyClass = Class
  private  
  public  
    const
      Const1 = TDeprecatedClass.Const1;
      Const2 = TDeprecatedClass.Const2;
      Const3 = TDeprecatedClass.Const3;
  end;

Whenever you use one of the constants from either TMyClass or TDeprecatedClass you will get a compiler warning.

Like I said it's not a very quick or practical approach but it gets the job done.
Now it's your choice if you want to mark them one by one or not :)

I hope this helped you a bit.

Timeserver answered 4/12, 2014 at 14:55 Comment(0)
R
3

You'll need to mark each variable as deprecated individually. For what it's worth, when the Currency & Date/Time formatting variables were deprecated in the RTL, each one was marked separately.

From the Delphi XE RTL Source:

var
  CurrencyString: string deprecated 'Use FormatSettings.CurrencyString';
  CurrencyFormat: Byte deprecated 'Use FormatSettings.CurrencyFormat';
  CurrencyDecimals: Byte deprecated 'Use FormatSettings.CurrencyDecimals';
  DateSeparator: Char deprecated 'Use FormatSettings.DateSeparator';
  TimeSeparator: Char deprecated 'Use FormatSettings.TimeSeparator';
  [...]
Rosenblum answered 4/12, 2014 at 15:35 Comment(0)
P
1

Depending on how your constants are laid out, and what you actually have inside your constants, there's a trick you can do in your code. It's simple rather. Highlight the entire block of constants, and open the Replace Text screen. For the Text to Find, put a semicolon ;. For Replace With, enter deprecated; (with a leading space). Then choose Replace All. It will simply replace all of the semicolons with the desired text. This is assuming you don't intend to put a custom message with each one, but if you have a common message, you can include that in the Replace With field.

Note this doesn't technically answer the question as it's written, but it does solve the underlying issue of deprecating many constants at once.

Paulin answered 6/12, 2014 at 1:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.