Recent

Author Topic: Please help me better understand this error ...  (Read 3629 times)

tetrastes

  • Hero Member
  • *****
  • Posts: 768
Re: Please help me better understand this error ...
« Reply #15 on: August 06, 2025, 04:09:07 pm »
The question is not about what typed constants are. It is about what is TBook at compile time and what aDefaultBookType is initialized with (what is "value embedded in the binary").

tetrastes

  • Hero Member
  • *****
  • Posts: 768
Re: Please help me better understand this error ...
« Reply #16 on: August 06, 2025, 04:13:39 pm »
And since their value isn't known at compile-time,
???
Code: Pascal  [Select][+][-]
  1. const
  2.     ii: integer = 2;
  3.  
Is value of ii not known at compile time?

gues1

  • Guest
Re: Please help me better understand this error ...
« Reply #17 on: August 06, 2025, 04:58:26 pm »
May be there are some confusion about:

as DEFAULT of a parameter of a method only a CONSTANT expression with a value is valid. These means that is not valid any value come from memory reference (like static variable) or any reference to a class, record, array or other things.

It must be defind as (i.e.):

Code: Pascal  [Select][+][-]
  1. const
  2.    SomeConst = ...... ;

EganSolo

  • Sr. Member
  • ****
  • Posts: 398
Re: Please help me better understand this error ...
« Reply #18 on: August 07, 2025, 02:54:07 am »
Khrys,

I echo Tetrastes and Gues1's concern.

If, as you said, TBook is not a constant but a pointer reference to some value which is not known at compile time, why does the compiler compile the expression:
Code: Pascal  [Select][+][-]
  1.   const
  2.     aDefaultBookType : TBookClass = TBook;    
  3.  

The key point here isn't the nature of aDefaultBookType. I think we're all agreed that's a static variable. What's at stake here is TBook. A typed constant accepts constant values. If, in that expression, TBook isn't a constant, what is it, and how come the compiler compiles that line?


Khrys

  • Sr. Member
  • ****
  • Posts: 456
Re: Please help me better understand this error ...
« Reply #19 on: August 07, 2025, 08:04:56 am »
A typed constant accepts constant values. If, in that expression, TBook isn't a constant, what is it, and how come the compiler compiles that line?

(emphasis mine)

What exactly do you mean by "constant values"?

Take a look at the syntax diagrams for ordinary constants and typed constants:

Quote from: Ordinary (untyped) constant

--> constant declaration --- identifier --- = --- expression --- hintdirectives --- ; --->
                          ^                                                            |
                          +------------------------------------------------------------+

Quote from: Typed constant

--> typed constant declaration --- identifier --- : --- type --- = --- typed constant --- hintdirectives --- ; --->
                                ^                                                                               |
                                +-------------------------------------------------------------------------------+

--> typed constant -------------+- constant ------------+----->
                                +- address constant ----+
                                +- array constant ------+
                                +- record constant -----+
                                +- procedural constant -+

Note how "address constant" (which  TBook  undoubtedly is, for reasons I've explained already) only applies to typed constants (aka static variables).
Also note how none of the extra types allowed in typed constants can be used as default parameter values. The following doesn't compile:

Code: Pascal  [Select][+][-]
  1. procedure Foo(const A: TStringArray = ('one', 'two'));
  2. procedure Bar(const P: TPoint = (X: 0; Y: 0));
  3. procedure Baz(P: TProcedure = @Abort);

I propose renaming ordinary constants to compile-time constants and typed constants to static variables, which would prevent this kind of confusion.

gues1

  • Guest
Re: Please help me better understand this error ...
« Reply #20 on: August 07, 2025, 08:45:53 am »
.....
You cannot use anything that use reference in memory. If you write "TStringArray = ('one', 'two')" this refer to a memory that the compiler doesn't know at compiler time. The default "value" cannot refer to record, class or any other structured data.

"Defaul values" are different concepts form CONST definitions.

If you can declare:

Code: Pascal  [Select][+][-]
  1. const
  2. A = B; //whitout nothing else

then you can use A as default value (I think).
Embarcadero explain "Default Values" in very simple mode (expecialy in what you cannot use).

Your suggestion to distinguish between real constants and static variables seems a bit excessive to me, although it's reasonable from a philosophical perspective.
This is actually the first time I've encountered the problem of default values. I've never had any doubts about what to set as defaults.

gues1

  • Guest
Re: Please help me better understand this error ...
« Reply #21 on: August 07, 2025, 09:09:36 am »
Code: Pascal  [Select][+][-]
  1.   const
  2.     aDefaultBookType : TBookClass = TBook;    
  3.  
The key point here isn't the nature of aDefaultBookType. I think we're all agreed that's a static variable. What's at stake here is TBook. A typed constant accepts constant values. If, in that expression, TBook isn't a constant, what is it, and how come the compiler compiles that line?

Explain clear about using that like DEFAULT VALUE: at compier time TBook, TBooKClass are NIL, nothing else (and I not agree with this since TBook and ... are definitions not hinstances). You can oppose that the compiler should accept that and use "nil" like default value, but I think it prefer that an error should be express like now, 'cause ambiguity.

EDIT: TBook is a concept, not a value. It will never be a value.
« Last Edit: August 07, 2025, 09:18:05 am by gues1 »

tetrastes

  • Hero Member
  • *****
  • Posts: 768
Re: Please help me better understand this error ...
« Reply #22 on: August 07, 2025, 09:53:11 am »
Note how "address constant" (which  TBook  undoubtedly is, for reasons I've explained already)

Can you show this explanation, please.

tetrastes

  • Hero Member
  • *****
  • Posts: 768
Re: Please help me better understand this error ...
« Reply #23 on: August 07, 2025, 10:18:21 am »
Explain clear about using that like DEFAULT VALUE: at compier time TBook, TBooKClass are NIL, nothing else (and I not agree with this since TBook and ... are definitions not hinstances). You can oppose that the compiler should accept that and use "nil" like default value, but I think it prefer that an error should be express like now, 'cause ambiguity.

EDIT: TBook is a concept, not a value. It will never be a value.

But
Code: Pascal  [Select][+][-]
  1. Type
  2.  
  3.   TBook = class
  4.   end;
  5.  
  6.   TLongBook = class(TBook)
  7.   end;
  8.  
  9.   TBookClass = class of TBook;
  10.  
  11. const
  12.     aDefaultBookType : TBookClass = TBook;
  13.  
  14. begin
  15.   writeln(HexStr(aDefaultBookType));
  16.   aDefaultBookType := TLongBook;
  17.   writeln(HexStr(aDefaultBookType));
  18. end.

outputs
Code: Bash  [Select][+][-]
  1. 00000010000E000
  2. 00000010000E0E0

So @Khrys is right, TBook and TLongBook are  "address constants", i.e. pointers, though I don't understand to what.

gues1

  • Guest
Re: Please help me better understand this error ...
« Reply #24 on: August 07, 2025, 10:37:31 am »
So @Khrys is right, TBook and TLongBook are  "address constants", i.e. pointers, though I don't understand to what.
Physically speaking, it's obvious that TBook exists somewhere in memory: for example, if you have a procedure defined as a "class procedure" within TBook, it is called and executed without instantiating TBook. This also applies when using RTTI, where I could refer to TBook without explicitly referring to the compiler.
But this has nothing to do with whether TBook can be defined as DEFAULT VALUE.

Edit: about the concept of "address constants": I would keep this concept well away from common usage. A pointer (or rather, what it refers to) can be valid for a limited "time," but it doesn't necessarily remain constant throughout the life of a program.
A trivial example is dynamic arrays, which can be modified and expanded, and any pointer certainly can't be defined as constant.
When we use the value of "@xxxx" at a given point in the program, it is valid within that context. But nothing generally tells us that that value remains valid outside of that context.
Outside, we should again refer to "@xxxx" and not to the value stored in some variable.
« Last Edit: August 07, 2025, 10:54:29 am by gues1 »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6398
  • Compiler Developer
Re: Please help me better understand this error ...
« Reply #25 on: August 08, 2025, 10:01:49 pm »
A constant expression consists of integers, floating point values, booleans, enums, sets and strings as well as untyped constants. Anything else is not a constant expression and thus can not be used as default parameter.
For variable and typed constant initialization additional expressions are possible (like class types or function pointers).
That's simply how things are.

 

TinyPortal © 2005-2018