Why is there no warning or hint that the constant is not used?

I tried to show the code to another person when I subtly realized that compiler hint messages are not used next to the declared variables, there are no hints or messages when the declared constant is not used. The following code is an example:

program Project1; {$APPTYPE CONSOLE} uses SysUtils, Math; const intM: Integer = 1000; var valorDouble, notusedvar: Double; begin try valorDouble := 0.001; Writeln('--- Codigo atual --'); Writeln('Double -> ', Trunc(valorDouble * 1000)); Writeln('--- Correcao?? --'); Writeln('Trunc(1.0000001) -> ', Trunc(1.0000001)); Writeln('Trunc(0.001 * 1000.0) -> ', Trunc(0.001 * 1000.0)); Writeln('Trunc(0.0010 * 1000.0) -> ', Trunc(0.0010 * 1000.0)); Writeln('Trunc(0.00100 * 1000.0) -> ', Trunc(0.00100 * 1000.0)); Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. 

Why is there no hint of an unused constant? Is there any logical explanation for this difference?

+8
compiler-construction compiler-warnings constants delphi
source share
2 answers

Let's go straight to the example. Let's say you write a DLL with 1 exported function. One of these functional parameters is an integer ...

 procedure DoSomething(const Value: Integer); stdcall; 

Now let's say that you have defined several constants to represent all possible integer values โ€‹โ€‹that this function can recognize ...

 const CON_ONE = 1; CON_TWO = 2; CON_THREE = 3; //Maybe hundreds 

Now tell me, when you implement this function, you really only need the first CON_ONE , but not the other two. Do you really want a hint of each of them?

A more realistic example are things like HKEY_LOCAL_MACHINE , HKEY_CURRENT_USER , etc. that come with Delphi (bound to Windows API calls). Just take a look at all the constants in Windows.pas . Imagine if all of these possible constants raised a compiler hint.

In fact, when you get a compiler hint for an unused variable, most often it means a coding error (or just something you forgot to delete), while an unused constant usually only means an unrealized opportunity.

+7
source share

Interest Ask.

For the constants (and variables) declared in the section of the interface of the block, it is quite easy to understand why unused instances do not cause compiler hints: everything in the interface section is published in the outside world for use, that the block cannot be in the know. As a unit programmer, you suggest / present possible values โ€‹โ€‹that users of your device can use in their code, although the device does not have to use them in its implementation. See Consts.pas , for example, a unit whose sole purpose is interface constants (ok, resourcestrings) for use elsewhere.

For the constants declared in the unit implementation section, and - as you can see from your code example - apparently, also for the constants declared in the program file, it seems to me that there is no clear reason why there is no hint of unused constants.

And as with an encore, the reason the initialized variables in the implementation section do not prompt is because they are actually a concatenation of the variable declaration and the assignment operator, so the initialized variable is used from the point of view of the compiler.

+4
source share

All Articles