Why are assignable typed constants disabled in my Delphi 7 by default?

Instead, I can just type

const clicks : Integer = 1; 

I need to enter

 {$J+} const clicks : Integer = 1; {$J-} 

I think it’s much easier to just check the box in the compiler options menu .. but I wanted to make sure that it didn’t hurt me in a long error .. and wondered why it would be disabled (Un-checked)

I appreciate any help with this, thank you guys.

+1
delphi delphi-7
source share
3 answers

They were disabled by default since Delphi 2, IIRC was released. The actual parameter was provided in a dialog box with Delphi 1, and I seem to recall the default noise, which was changed from enabled to disabled in the next version. It was a long time, so I could be disconnected alone when the default was overridden; it could be D3. Assigned constants are a carryover from the old days of Turbo Pascal and replace the absence of the actual type of the static variable. They have been replaced by a better solution, which are initialized (global) variables (see Declaring Variables ).

If you really need to use a writable constant, you should only do this in the smallest (most restricted) area where you need it, instead of changing the global settings. Constants should be exactly where possible (constant). A typical reason for using constants for writing is when you need a local variable in a certain area (for example, a procedure, function or unit) that needs to maintain its value between calls, and this is usually the best way to do this. Some of the parameters are object fields (member variables) or variables with a limited scope (variables that are visible in limited areas, for example, in the implementation section of a block, are initialized with the initial value).

Assignable constants mean that the value can be changed at run time and is really rarely useful. True constants are that which is constant and should not be allowed to change.

The same can be said for typed constants; there must be an urgent need to use them, and it rarely happens if you do not store a constant array, record, pointer, or procedural type, as in these declarations:

 const TIntLookupArray: array[0..1] of Integer = (1, 2); TErrorMsgs: array[0..1] of string = ('Invalid input', 'Invalid sequence'); 

Is there a reason you use typed constants? You will not have this problem if you just use

 const clicks = 1; 

and let the compiler decide the correct type. If you want to make sure this is an Integer size, just use a type like

 const clicks = Integer(1); 

See the Delphi documentation for more information.

+4
source share

I assume it was disabled because the "assignable constants" are the contact itself. You can probably replace your code with

 var clicks : Integer = 1; 

The only situation where “assigned constants” are a little useful is to model a static local variable C.

+3
source share

The assignable typed constants option is disabled by default as long as I remember. It was disabled by default in many versions prior to Delphi 7.

The language function is poorly designed, and because of this, in my opinion, should not be used. This is misleading to code readers. The idea of ​​changing a constant is just plain weird. The use case for assignable typed constants is variables with local coverage with static (i.e. global) storage duration.

If the language was correctly designed, then there would be room for locally restricted variables with static storage duration. But the design was deadly wrong because you cannot easily distinguish between assignable typed constants and real constants in Delphi due to the const keyword overload.

A smart design would have to introduce syntax for declaring variables with static storage and distinguish them from constants. But instead, designers chose a compiler option. Or maybe again when all typed constants were assigned in Turbo Pascal. Anyway, without language syntax support, overloading the const keyword is simply untenable.

The compiler option is saved for backward compatibility. You should not use assignable typed constants. Again, as far as I remember, any decent coding standard prohibits the use of assignable typed constants.

My recommendations:

  • Disable assignable typed constants globally.
  • Never use $ J + in your code.

The closest construct in modern Delphi for locally bounded variables with static storage duration is strict private class variables. They are not available in Delphi 7, so your only option is a global variable, which is a rather miserable situation.

+3
source share

All Articles