1

Instead of me being able to just type

const clicks : Integer = 1;

I have to type

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

I think its alot easier to just checked the box in the compiler options menu.. but i wanted to make sure that it wouldnt hurt me in the long wrong.. and was wondering why it would be disabled (Un-checked)?

I appreciate any help on this, thank you guys.

Bruce McGee
  • 15,076
  • 6
  • 55
  • 70
Sheep
  • 311
  • 2
  • 9

3 Answers3

4

The default has been to disable them since Delphi 2 was released, IIRC. The actual option was provided in the dialog with Delphi 1, and I seem to recall a fuss about the default having changed from enabled to disabled in the next version. It's been a long time though, so I could be off by one on when the default was reversed; it may have been D3. Assignable constants are a carry-over from the old Turbo Pascal days, and are a substitute for the lack of an actual static variable type. They were replaced by a better solution, which is initialized (global) variables (see Declaring Variables).

If you have an actual need to use a writable constant, you should do so only on the smallest (most limited) scope where it's necessary, rather than changing the global setting. Constants should be exactly that where possible (constant). The typical reason for using writable constants is when you require a local variable within a certain scope (such as a procedure, function, or unit) that needs to keep its value between calls, and there is usually a better way to do so. Some of the options are object fields (member variables), or limited scope initialized variables (variables visible in restricted areas, such as within the implementation section of a unit, and initialized to a starting value).

Assignable constants means the value can be changed at runtime, and is really rarely useful. True constants are just that - constant - and shouldn't be allowed to change.

The same can be said for typed constants; there should be an actual need for using them, and there rarely is unless you're storing 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're using typed constants in the first place? You won't have this problem if you just use

const clicks = 1;

and let the compiler decide the proper type. If you want to make sure it's the size of an Integer, just use a typecast like

const clicks = Integer(1); 

See the Delphi documentation docwiki for more info.

Ken White
  • 123,280
  • 14
  • 225
  • 444
3

I guess it has been disabled because "assignable constants" is a contadiction in itself. You can probably replace your code by

var
  clicks : Integer = 1;

The only situation where "assignable constants" are slightly useful is to simulate a static local C variable.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
  • When i change it to var , i get the error cannot initialize local variables – Sheep May 30 '13 at 18:27
  • Ok, well i moved it into the implementation section and it worked great. Now are there any advantages/dis-advantages to doing this? – Sheep May 30 '13 at 18:38
  • @Sheep: It doesn't matter if it's in the interface section or the implementation section; what matters is that only *global* variables can be assigned on declaration, not procedure-local variables or fields. – Andreas Rejbrand May 30 '13 at 18:46
  • 2
    @Nick Please justify that. In Delphi 7, how would you recommend creating a variable with static storage duration? – David Heffernan May 30 '13 at 19:42
3

The assignable typed constants option has been disabled by default for as long as I can remember. It was disabled by default in many versions preceeding Delphi 7.

The language feature is poorly designed and because of that, in my view, should not be used. It is confusing to readers of the code. The idea of modifying a constant is quite simply bizarre. The use-case for assignable typed constants is locally scoped variables with static (i.e. global) storage duration.

If the language had been properly designed then there would be a place for locally scoped variables with static storage duration. But the design was fatally flawed because you cannot, in the Delphi language, readily distinguish between assignable typed constants and real constants, due to the overloading of the const keyword.

A sane design would have introduced syntax to declare variables with static storage and distinguish them from constants. But instead the designers chose a compiler option. Or perhaps, way back when, in Turbo Pascal all typed constants were assignable. All the same, without language syntax support, the overloading of the const keyword is simply untenable.

The compiler option is retained for backwards compatibility. You are not expected to use assignable typed constants. Again, for as long as I can remember, any decent coding standard bans the use of assignable typed constants.

My recommendations:

  1. Disable assignable typed constants at the global level.
  2. Never use $J+ in your code.

The closest construct in modern Delphi for locally scoped variables with static storage duration is strict private class variables. They are not available in Delphi 7 and so your only option is a global variable, which is a rather sorry state of affairs.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490