Lazarus

Programming => General => Topic started by: GypsyPrince on March 31, 2020, 01:36:21 am

Title: [SOLVED] Assign existing constants to new constants...
Post by: GypsyPrince 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?
Title: Re: Assign existing constants to new constants...
Post by: eljo on March 31, 2020, 01:40:49 am
try removing the : string definition from your constants and see if that helps.
Title: Re: Assign existing constants to new constants...
Post by: GypsyPrince 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.

Title: Re: Assign existing constants to new constants...
Post by: eljo 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.
Title: Re: Assign existing constants to new constants...
Post by: GypsyPrince 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...
Title: Re: Assign existing constants to new constants...
Post by: eljo 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.
Title: Re: Assign existing constants to new constants...
Post by: GypsyPrince on March 31, 2020, 02:31:37 am
Oh yes, I forgot. Constants are mutable by default in Free Pascal.
Title: Re: Assign existing constants to new constants...
Post by: PascalDragon 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.).
Title: Re: Assign existing constants to new constants...
Post by: Thaddy 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 )
Title: Re: [SOLVED] Assign existing constants to new constants...
Post by: GypsyPrince 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.
Title: Re: [SOLVED] Assign existing constants to new constants...
Post by: PascalDragon 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