3

this site : http://www.drbob42.com/delphi/wizards.htm

showed a very puzzling code at the bottom

 unit ShareMem;
 { (c) 1997 by Bob Swart (aka Dr.Bob - http://www.drbob42.com }
 interface

 const
...
 uses
   Windows;

 const
   Handle: THandle = 0;
...
 function GetCommandLine: PChar; stdcall;
   external 'kernel32.dll' name 'GetCommandLineA';
...
   begin
     Handle := LoadLibrary('BCBMM.DLL');
 end.

how could this be ?

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
none
  • 4,669
  • 14
  • 62
  • 102
  • possible duplicate of [In Delphi 7, why can I assign a value to a const?](http://stackoverflow.com/questions/48934/in-delphi-7-why-can-i-assign-a-value-to-a-const) – Rob Kennedy Dec 02 '10 at 14:39

3 Answers3

6

Delphi has something called assignable consts which allows a const value to be assigned. This can be turned on/off through compiler directives and switches. For a longer answer see here.

It sometimes comes in handy in times before class properties were possible. Even if the const is declared inside a function, it keeps its value between calls.

procedure Test;
{$WRITEABLECONST ON}
const
  AssignableConst: Integer = 0;
{$WRITEABLECONST OFF}
begin
  AssignableConst := AssignableConst + 1; 
  WriteLn('Test is called ' + IntToStr(AssignableConst) + ' times'); 
end;
Community
  • 1
  • 1
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
4

A typed const, by default (Edit: as noted by Rob in comments, this was changed to no longer be the default years ago), is more like a static variable. You can turn this behavior off with a compiler directive.

This was commonly used as a substitute for class/static properties in old versions of Delphi. Now that Delphi actually has that feature, there is no good reason to do this IMHO.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • could you add to your answer a link to explanation of delphi current class static feature? – none Dec 02 '10 at 13:49
  • Craig, the default for writeability changed many versions ago (in D7, I think). But even when the compiler disallows assignment statements to them, they're still like static variables. They always have addresses, and they're still not allowed in constant expressions. Writeable typed constants aren't really replaced by class variables. We've never needed writeable typed consts at unit-level scope; we've been able to use ordinary variables there. Where writeable typed consts were *and still are* useful is for *local* static variables. – Rob Kennedy Dec 02 '10 at 14:39
  • 1
    @Rob, good point about the default; I'll update that. Regarding the implementation details, you're right, but I don't think he was asking about that. I like the reduced scope of an assignable const, but I don't think it's worth the readability pain evidenced by this question, so I don't use them as local static variables; I just live without that feature. – Craig Stuntz Dec 02 '10 at 14:42
1

What you wondering about is a writable typed constant. Typed constants were writable since old days of Turbo Pascal. In the fact, it was the only way to declare an initialized variable. Internally, writable typed constants and initialized variables are equivalent, both go into DATA segment (thats how Lars Truijens's example work). Also, typed constants can hold data types which disallowed for true constants, what is the true semantic purpose of them. Since Delphi 4 (or 3 even?) Borland figured out what it is weird to mix constants and variables that way, and introduced initialized global variables and $WRITEABLECONST switch directive (OFF by default). Initialized variables cannot appear in the local scope, thus there is still purpose for writable typed constants to exist.

Free Consulting
  • 4,300
  • 1
  • 29
  • 50