Jump to content
Tommi Prami

Constant declarations keeps baffling me (don't know enough I guess)

Recommended Posts

Helllo,

Defining constants is pretty limited in delphi. some I think, related to single pass compiler.

Latest was this case

  // Works
  MULTIPLIER = 1000;
  MULTIPLIED_CONSTANT = 1E-11 * MULTIPLIER;
  OTHER_FLOAT_CONSTANT: Double = 0.005;

  // Don't work
  MULTIPLIER: NativeInt = 1000;
  MULTIPLIED_CONSTANT = 1E-11 * MULTIPLIER; // <- E2026 Constant expression expected 

This seems kind of weird.

Is this limitation or bug in the Compiler, or feature I don't know about.

 

-Tee-
 

Share this post


Link to post
36 minutes ago, Tommi Prami said:

Is this limitation or bug in the Compiler, or feature I don't know about. 

 

From documentation " Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions. ".

Try

MULTIPLIER = NativeInt(1000);

 

Share this post


Link to post

Only untyped constants are "real" constants. Typed constants are more like variables you can't change. That's just how Delphi works.

Share this post


Link to post
4 hours ago, uligerhardt said:

Only untyped constants are "real" constants. Typed constants are more like variables you can't change. That's just how Delphi works.

yes, so called "CONSTANT VARIABLES" 😉

 

  • Haha 1

Share this post


Link to post

Compiler error

var
  foo: Integer = 10; //E2195 Cannot initialize local variables
begin
  foo := 20;

This compiles

{$WRITEABLECONST ON}
const
  foo: Integer = 10;
begin
  foo := 20;

 

Edited by Cristian Peța

Share this post


Link to post
54 minutes ago, Cristian Peța said:

This compiles


{$WRITEABLECONST ON}
const
  foo: Integer = 10;
begin
  foo := 20;

But at next invocation of such function foo is already 20. So typed const are like static variables in that sense.

Also, in past Delphi versions they were writable by default (Turbo Pascal compatibility).

Share this post


Link to post

How about an untyped Multiplier and

MULTIPLIED_CONSTANT = 1E-11 * NativeInt(MULTIPLIER);

?

  • Like 1

Share this post


Link to post
1 hour ago, Cristian Peța said:

Compiler error


var
  foo: Integer = 10; //E2195 Cannot initialize local variables
begin
  foo := 20;

This compiles


{$WRITEABLECONST ON}
const
  foo: Integer = 10;
begin
  foo := 20;

 

Clever way of introducing thread-unsafety 😉

  • Like 1
  • Haha 1

Share this post


Link to post
4 minutes ago, Stefan Glienke said:

Clever way of introducing thread-unsafety

I wonder when anyone comes up with a request for threadconst:classic_huh:

  • Haha 3
  • Confused 1

Share this post


Link to post
3 hours ago, Cristian Peța said:

Compiler error


var
  foo: Integer = 10; //E2195 Cannot initialize local variables
begin
  foo := 20;

 

 

It is true that variables can't be initialized when using the 'var' block at the top of the function.  But inline variables can be initialized:

begin
  var foo: Integer := 10; // OK
  • Like 1

Share this post


Link to post

Did not consider thsat they are those hybrid "varconsts", if type defined.

If type could be defined for const could make some code cleaner. Make sure, some constant is always Double, not extended, in 32bit and also in 64bit...


But anyways, thanks for info y'all...
 

-tee-

Share this post


Link to post
3 hours ago, Tommi Prami said:

If type could be defined for const could make some code cleaner. Make sure, some constant is always Double, not extended, in 32bit and also in 64bit...

You can use cast syntax - not sure how much that helps:

const
  OTHER_FLOAT_CONSTANT Double0.005);
Edited by uligerhardt

Share this post


Link to post
7 hours ago, uligerhardt said:

You can use cast syntax - not sure how much that helps:


const
  OTHER_FLOAT_CONSTANT Double0.005);
const
  OTHER_FLOAT_CONSTANT = Double(0.005);

 

Share this post


Link to post
2 hours ago, Remy Lebeau said:

const
  OTHER_FLOAT_CONSTANT = Double(0.005);

 

What problem does this solve? 

Share this post


Link to post
9 minutes ago, David Heffernan said:

What problem does this solve? 

I was simply fixing the broken syntax in uligerhardt's reply.

Share this post


Link to post
8 minutes ago, Remy Lebeau said:

I was simply fixing the broken syntax in uligerhardt's reply.

Does it even compile, and if so what does it mean? 

Share this post


Link to post
17 hours ago, Remy Lebeau said:

const
  OTHER_FLOAT_CONSTANT = Double(0.005);

 

Hmm, my code got garbled somehow. Thanks for the correction.

Share this post


Link to post
14 hours ago, David Heffernan said:

Does it even compile, and if so what does it mean? 

My fragment wouldn't have compiled. And as stated I'm not sure if it helps with this:

On 9/27/2024 at 6:16 AM, Tommi Prami said:

Make sure, some constant is always Double, not extended, in 32bit and also in 64bit...

That might depend on the scenario. IIRC this syntax is used in the RTL/VCL code - can't check right now.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×