Recent

Author Topic: [SOLVED] Assign existing constants to new constants...  (Read 463 times)

GypsyPrince

  • Guest
[SOLVED] Assign existing constants to new constants...
« on: March 31, 2020, 01:36:21 am »
Does Lazarus/ObjectPascal allow for assigning an existing constant to a new constant as shown here?

Code: Pascal  [Select][+][-]
  1. m_HLP_ENG_CNCL : String = 'Procedure Execution';
  2. m_HLP_ENG_FAIL : String = m_HLP_ENG_CNCL;

When I do this I get the error "Illegal expression".  Is this allowed? Or, might I just have my syntax incorrect?
« Last Edit: April 01, 2020, 07:40:38 am by GypsyPrince »

eljo

  • Sr. Member
  • ****
  • Posts: 407
Re: Assign existing constants to new constants...
« Reply #1 on: March 31, 2020, 01:40:49 am »
try removing the : string definition from your constants and see if that helps.

GypsyPrince

  • Guest
Re: Assign existing constants to new constants...
« Reply #2 on: March 31, 2020, 01:53:26 am »
@eljo

Quote
try removing the : string definition from your constants and see if that helps.

Already tried. Same error.


eljo

  • Sr. Member
  • ****
  • Posts: 407
Re: Assign existing constants to new constants...
« Reply #3 on: March 31, 2020, 01:58:38 am »
@eljo

Quote
try removing the : string definition from your constants and see if that helps.

Already tried. Same error.
the folowing is a simple program just created and executed in my windows 7 installation.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. const
  4.   m_HLP_ENG_CNCL = 'Procedure Execution';
  5.   m_HLP_ENG_FAIL = m_HLP_ENG_CNCL+'. Ends here';
  6.  
  7. begin
  8.   writeln(m_HLP_ENG_CNCL);
  9.   WriteLn(m_HLP_ENG_FAIL);
  10. end.
  11.  
  12.  

No problems reported everything prints correctly in the console window.

GypsyPrince

  • Guest
Re: Assign existing constants to new constants...
« Reply #4 on: March 31, 2020, 02:14:56 am »
I had to remove the type definition from both the new constants and the existing constants from which their value is assigned. It works now.  I wonder if it's a design flaw in Free Pascal or if the developers intended it that way...

eljo

  • Sr. Member
  • ****
  • Posts: 407
Re: Assign existing constants to new constants...
« Reply #5 on: March 31, 2020, 02:16:43 am »
as far as I remember its by design, something about different types of constants, never cared enough to remember it.

GypsyPrince

  • Guest
Re: Assign existing constants to new constants...
« Reply #6 on: March 31, 2020, 02:31:37 am »
Oh yes, I forgot. Constants are mutable by default in Free Pascal.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1695
  • Compiler Developer
Re: Assign existing constants to new constants...
« Reply #7 on: March 31, 2020, 09:36:25 am »
I had to remove the type definition from both the new constants and the existing constants from which their value is assigned. It works now.  I wonder if it's a design flaw in Free Pascal or if the developers intended it that way...

Constant and variable initializers can only use untyped constants and some internal functions (e.g. High, Ln, etc.).

Thaddy

  • Hero Member
  • *****
  • Posts: 10189
Re: Assign existing constants to new constants...
« Reply #8 on: March 31, 2020, 09:42:17 am »
And untyped constant is immutable. (as well as any typed constant declared in {$J-} state, I might add )
« Last Edit: March 31, 2020, 09:44:02 am by Thaddy »
I am more like donkey than shrek

GypsyPrince

  • Guest
Re: [SOLVED] Assign existing constants to new constants...
« Reply #9 on: April 01, 2020, 07:54:59 am »
@PascalDragon and @Thaddy

Quote
And untyped constant is immutable. (as well as any typed constant declared in {$J-} state, I might add )

Quote
Constant and variable initializers can only use untyped constants and some internal functions (e.g. High, Ln, etc.).

These two points were proving to be an annoyance for me for a brief time because I generally prefer strongly typed data fields. However, after tinkering a bit more, I am beginning to feel a little more at ease with untyped fields.

PascalDragon

  • Hero Member
  • *****
  • Posts: 1695
  • Compiler Developer
Re: [SOLVED] Assign existing constants to new constants...
« Reply #10 on: April 01, 2020, 09:58:15 am »
These two points were proving to be an annoyance for me for a brief time because I generally prefer strongly typed data fields. However, after tinkering a bit more, I am beginning to feel a little more at ease with untyped fields.

You can enforce a specific type using a typecast:

Code: Pascal  [Select][+][-]
  1. const
  2.   MyString = UnicodeString('Hello World');
  3.   MyDWord = DWord(42);

And if you don't need to initialize another constant with it you can just as well use typed constants (and disable writeable constants).

 

TinyPortal © 2005-2018