Recent

Author Topic: iniline var in Delphi vs const var // Re: Conditional ternary operators For FreePascal?  (Read 2166 times)

Warfley

  • Hero Member
  • *****
  • Posts: 2066
I'm pretty sure that it will work in Delphi, because Delphi already supports inline constants:
Code: Pascal  [Select][+][-]
  1.   const a = Random(100); // Runtime assigned constant
  2.   a:=42; // Error because a is constant

So yeah, FPC does not support it, because FPC does not support inline variables (and thereby also inline consts), but Delphi will support this, which is why I said it makes sense for Delphi

gues1

  • Guest
I'm pretty sure that it will work in Delphi, because Delphi already supports inline constants:
Code: Pascal  [Select][+][-]
  1.   const a = Random(100); // Runtime assigned constant
  2.   a:=42; // Error because a is constant
So yeah, FPC does not support it, because FPC does not support inline variables (and thereby also inline consts), but Delphi will support this, which is why I said it makes sense for Delphi

That is supported ONLY like "inline variable" like I told. In other cases Delphi is like FPC, it is not supported ... the same (pretty sure) will be the usage of ternary function.

Take care that inline vars are local and normally used inside logical blocks. They are very very usefull for debugging too.
The consts defined in such way have very limited visibility.

korba812

  • Sr. Member
  • ****
  • Posts: 483
I'm pretty sure that it will work in Delphi, because Delphi already supports inline constants:
Code: Pascal  [Select][+][-]
  1.   const a = Random(100); // Runtime assigned constant
  2.   a:=42; // Error because a is constant
Is that possible too?
Code: Pascal  [Select][+][-]
  1. const a = Random(100);
  2. var b: array[0..a] of Byte;
  3.  

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
I don't know if those "const" construct actually works in Delphi (or if they just refer to C or some other language). Or maybe they are just a "would be nice" hope....

But using "const" for the described concept is wrong. Well, ironically, in the history of Pascal it seems the perfect fit....
That is, in Pascal, "const" is already be used for variables (typed constant). At least with some modes... So lets add more non-constant declarations to that keyword, it is already messed up.

Anyway, a constant is a value that does not change.

A variable that can be assigned once (as initialization) each time the surrounding function/scope is entered, and then not be changed, that is called immutable. Mixing it with "const", IMHO just is confusing.




Given that according to the description, it appears that the Warfley meant an immutable value in "const a := random", then that would make me think that the array def would not work, based on that "a" is not a constant.

In the same way, as you can't do
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. const a: integer = 10;
  3. type b = array[0..a] of byte; // error
because "a" isn't a constant.

gues1

  • Guest
I don't know if those "const" construct actually works in Delphi (or if they just refer to C or some other language). Or maybe they are just a "would be nice" hope....
@Warfley and me were spoken about Delphi.

In Delphi const with runtime value (like Random(10)) can be used ONLY in inline vars, so locally in the body of to methods.

Like with FPC, Delphi cannot use those in usual way decalration (in Interface section, Implementation and in the Methods declaration).

Delphi and FPC works in the same way (except inline vars).

korba812

  • Sr. Member
  • ****
  • Posts: 483
It appears to be different from regular typed constants, which are initialized once at program startup and is like a global variable. Rather, it is a local variable that is immutable once initialized.
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Inline_Variable_Declaration#Inline_Constants

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
It appears to be different from regular typed constants, which are initialized once at program startup and is like a global variable. Rather, it is a local variable that is immutable once initialized.
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Inline_Variable_Declaration#Inline_Constants

Yes, typed constants are basically variables (with some initialization). They can be changed at any time, just as variables can. (there is a modeswitch to disable that).

"inline const" is an immutable variable. It can't be changed once assigned. But it can be assigned a value (a different value) on its initialization, each time the scope is entered.



Looking at that doc, made me think. "For loop" counters are immutable inside the loop (they can only be changed by the loop, that is each time the scope of the iterated code is entered.
So really it should be
Code: Pascal  [Select][+][-]
  1. for const i: integer := 0 to foo do begin {i is immutable here} end;

I wonder what Delphi does, if you give it code like that?

gues1

  • Guest
Yes, typed constants are basically variables (with some initialization). They can be changed at any time, just as variables can. (there is a modeswitch to disable that).
"inline const" is an immutable variable. It can't be changed once assigned. But it can be assigned a value (a different value) on its initialization, each time the scope is entered.
Looking at that doc, made me think. "For loop" counters are immutable inside the loop (they can only be changed by the loop, that is each time the scope of the iterated code is entered.
So really it should be
Code: Pascal  [Select][+][-]
  1. for const i: integer := 0 to foo do begin {i is immutable here} end;
I wonder what Delphi does, if you give it code like that?

I don't try, but of course you cannot compile a code like this in Delphi. "Const", also used like inline, is always a const so you cannot use as index in a for loop.

You can use "var" instead of "const". And the syntax is different: in a "FOR .. TO .. DO" loop the variable must be an ordinal type, so you cannot explicit declare the type of this inline var ... the compiler infer it automatically.

Code: Pascal  [Select][+][-]
  1. for var index := Random(10) to 10 do

The compiler infer the variable index as integer in this case and

Code: Pascal  [Select][+][-]
  1. for var index := Random(10) to INT64(10) do

in this case it inrfer like INT64 type.
« Last Edit: August 09, 2025, 02:28:58 pm by gues1 »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
I don't try, but of course you cannot compile a code like this in Delphi. "Const", also used like inline, is always a const so you cannot use as index in a for loop.

Question can you do

Code: Pascal  [Select][+][-]
  1. var i := 0;
  2. while i < 10 do begin
  3.   const x := i+1;
  4.   writeln(x);
  5. end;  

Because within the loop, x is not modified once it was assigned.

If the loop was calling "foo(i)" and "procedure Foo(i: integer)" did have "const x := i+1;" => the foo would be called 10 times, and x would have 10 values. (despite being constant).

But back to the sample code, if that works (and, if "const" in Delphi works like similar constructs in other languages) then it really should), then where is the difference to "for const i := 1 to 11" ? (Well, syntax wise I know where the diff is...)

gues1

  • Guest
Why do "for" and "while" and "repeat" too exist ?

In the for cycle the "index" is immutable by code (except some hacks), not like value.

In the while you can do what you want, and use it for other things (like repeat / until).

Code: Pascal  [Select][+][-]
  1. var i := 0;
  2. while i < 10 do begin
  3.   const x := i+1;
  4.   writeln(x);
  5. end;  

In this case const is assigned only one time, the first time that enter in the block and so it has and maintain the const value of 1.

Why one want use this and not the standard way ? It's like I want to use "for" and not "while" or I want to use a property of a class or directly a function / procedure.

Like I told every thing should used with "brain". And everyone has his proper brain.

I would to add other things but your last post suggest me that is not necessary. We are talking about things that not exist in FPC. In Delphi they were and are used by many of developers.

If you notice, some thirdy party packets are written for Delphi with inline vars and for FPC with normal way (with too much {$IFDEF ....} I think).

 

TinyPortal © 2005-2018