Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: All constants are constant, but some are more constant than others?

Tags:

Consider:

const     clHotlight: TColor = $00FF9933;    clLink = clHotLight; //alias of clHotlight  [Error] file.pas: Constant expression expected 

and the alternate wording that works:

const     clHotlight = TColor($00FF9933);    clLink = clHotLight; //alias of clHotlight 

Explain.


Then consider:

const     AdministratorGUID: TGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}';    SuperuserGUID = AdministratorGUID; //alias of AdministratorGUID  [Error] file.pas: Constant expression expected 

And fix.

Edit: Added keyword const before declarations; someone didn't believe they were const.

like image 608
Ian Boyd Avatar asked Apr 26 '10 14:04

Ian Boyd


People also ask

What is a constant variable in Delphi?

A const parameter clearly tells the reader that the subroutine does not change the parameter's value. This improves the readability and clarity of the code. The compiler enforces the restriction. If you accidentally try to assign a new value to a const parameter, the compiler issues an error message.

How the constant are declared?

The const keyword Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.

Do constants need memory?

It depends. const int a = 5; Will take four bytes of memory (or however many bytes an int takes up on your system).

Why would you use a constant over a variable?

Constants are used when you want to assign a value that doesn't change. This is helpful because if you try to change this, you will receive an error. It is also great for readability of the code. A person who reads your code will now know that this particular value will never change.


2 Answers

clHotlight: TColor = $00FF9933; is not a constant but a typed constant (=static variable), i.e. the compiler reserves a slot in memory for a TColor which will hold the value $00FF9933 initially at run time.
Because that value can be changed later (with the Assignable Const option ON), it is not a real constant and cannot be accepted by the compiler in clLink = clHotLight;

clHotlight = TColor($00FF9933); is strictly the same as clHotlight = $00FF9933;
It is a true constant and the compiler will replace clHotlight by its value $00FF9933 wherever it appears in the code. And for clLink as well.

Read on this SO question (In Delphi 7, why can I assign a value to a const?) and all the good answers there...

EDIT: about TGUID...
The problem is that writing AdministratorGUID: TGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}'; is not proper.
It is using some compiler magic to call StringToGUID behind the scene, allowing the convenience to express the GUID as a string which they are not by nature. They are records.

So, trying AdministratorGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}'; will not work. That is not a GUID...

A workaround is to have a typed constant and variables pointing to the same memory area using the absolute directive:

const    AdministratorGUID: TGUID = '{DE44EEA0-6712-11D4-ADD4-0006295717DA}'; var    SuperuserGUID: TGUID absolute AdministratorGUID; //alias of AdministratorGUID    RootGUID: TGUID absolute AdministratorGUID;      //alias of AdministratorGUID 
like image 60
Francesca Avatar answered Jan 04 '23 00:01

Francesca


I tried this code :

  const     CAnswer1 = 42;     CAnswer2 : Integer = 42;    var     LAnswer : Integer;    begin     LAnswer := CAnswer1;     LAnswer := CAnswer2;   end; 

and here is the produced code :

Project9.dpr.18: LAnswer := CAnswer1; 004101AC C7056C6E41002A00 mov [$00416e6c],$0000002a //<- assign a hard-coded "42" value Project9.dpr.19: LAnswer := CAnswer2; 004101B6 A1701C4100       mov eax,[$00411c70] //<- fetch a variable's content 004101BB A36C6E4100       mov [$00416e6c],eax //<- assign this content  

You are right : some constants are more constant than others. The second constant is actually treated by the compiler as a variable.

like image 45
LeGEC Avatar answered Jan 04 '23 00:01

LeGEC