Jump to content
PiedSoftware

Delphi should let me use a const array reference as a constant

Recommended Posts

Posted (edited)
type
  TCodeStructure = record
    StartTkn: string;
    EndTkn: string;
    SubTkns: array of string;
  end;

type TIfTkns = (itElse, itElsif);

const elseTkn  = '#else';
      elsifTkn = '#elseif';
      IfTkns: array[TIfTkns] of string = (elseTkn, elsifTkn);

  IfStruct: TCodeStructure =
    (StartTkn: '#if';
     EndTkn:   '#endif';
     SubTkns: [IfTkns[itElse], IfTkns[itElsif]]); 

I wrote this code and got the error "E2026 Constant expression expected" on the last line.


I also tried 

SubTkns: IfTkns);

 

When I changed it to 

SubTkns: [elseTkn, elsifTkn]);

it compiled happily.
and that gave me E2010 Incompatible types: 'Dynamic array' and 'Array'.

To me it seems that Delphi should allow the first thing I tried - referencing a const array via constant indices to specify a constant. There is no reason to disallow that sort of thing. Ideally even the second attempt should be reasonable.

I have Delphi 10.4.

Edited by PiedSoftware

Share this post


Link to post

A typed constant (that's what your IfTkns constant is) is actually implemented as a compiler-initialized variable, and that's the reason you cannot use it as a constant expression. It may be counter-intuitive but that's the way it works and you have to live with it.

Share this post


Link to post

There's all sorts of things that Delphi should allow as constants, but that's a long known area of significant weakness in the language. 

Share this post


Link to post

Yes, David, I agree. I would like them to fill that out: everything that is logically able to be evaluated at compile time should be assignable to a constant.

Share this post


Link to post
7 hours ago, PiedSoftware said:

Yes, David, I agree. I would like them to fill that out: everything that is logically able to be evaluated at compile time should be assignable to a constant.

We'd all like that. Prospects are not good for it happening.

Share this post


Link to post
Posted (edited)

I guess I am a bit spoiled playing with Swift. It lets you assign any expression to a constant, compile time or run time.

Edited by PiedSoftware
  • Like 1

Share this post


Link to post
On 4/1/2024 at 12:54 AM, PiedSoftware said:

expression to a constant, compile time or run time

A constant you can assign at run time? Is that a one-off thing - assign once and then it's immutable? If not, then it's a variable, not a constant or have I misunderstood?

Share this post


Link to post
3 minutes ago, Ian Barker said:

A constant you can assign at run time? Is that a one-off thing - assign once and then it's immutable? If not, then it's a variable, not a constant or have I misunderstood?

:classic_laugh: Constant when you write the code, variable when the code is running :classic_laugh:

Share this post


Link to post
Posted (edited)

That wouldn't be strange in C++ ... 

int i = 42;

int main()
{
   const int  j = i; 
   const int* p = &j;
}
Edited by pmcgee

Share this post


Link to post
1 hour ago, Ian Barker said:

A constant you can assign at run time? Is that a one-off thing - assign once and then it's immutable? If not, then it's a variable, not a constant or have I misunderstood?

Imagine you could write this code in Delphi:

 

var
  i: Integer = 42;
begin
  const j = i;
  Writeln(j);
end.

Oh, wait, you can.

 

Too bad that the syntax is not really clean as it mimics the const declaration using the equal sign while it really is what other languages call immutable variable and thus should have used the assign operator but people with even less expertise in programming language design than me disagreed.

  • Like 2

Share this post


Link to post
Posted (edited)

In Delphi is allowed to change typed const, look here ... simple, if the relative option is set. Never used ... is terrible...

 

image.thumb.png.b9b4534203890f9910cd550118704cac.png

Edited by DelphiUdIT

Share this post


Link to post
22 minutes ago, Brandon Staggs said:

Oh yes, I remember something about Delphi7 ... but now these things should never be present, neither like options ... a code refactoring is a must.

We are talking in these days about safe memory .... and there are still those "options".

 

But this is my personal opinion.

 

Share this post


Link to post

The way Swift works is that you can declare value with either the keyword let or the keyword var. If you use let the value cannot be changed. But the expression you assign to a let value can be anything available at that point.

  • Like 1

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

×