Lazarus

Free Pascal => General => Topic started by: 440bx on June 18, 2022, 05:07:27 pm

Title: refer to writable constant using different names
Post by: 440bx on June 18, 2022, 05:07:27 pm
Hello,

I'm trying to refer to a writable constant using different names, for instance:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. program _Constants;
  4.  
  5. const
  6.   SOME_TEXT  : pchar = 'some text';
  7.  
  8. const
  9.   OTHER_TEXT = SOME_TEXT;          { illegal expression }
  10.   OTHER_TEXT : pchar = SOME_TEXT;  { got SYSTEM.PChar expected SYSTEM.PChar }
  11.  
  12. begin
  13. end.
  14.  
Basically, I want to be able to sometimes refer to the "constant" SOME_TEXT using the name OTHER_TEXT but, everything I've tried so far makes the compiler unhappy.

On a different note, the error message on the second option doesn't really help much. "got SYSTEM.PChar expected SYSTEM.PChar", it's a bit disconcerting to have the compiler complain that it got what it expected (usually that's not an error ;) )

as the signature states, FPC v3.0.4 (haven't tested with later versions)
Title: Re: refer to writable constant using different names
Post by: MarkMLl on June 18, 2022, 05:16:24 pm
Use a macro and be done with it.

MarkMLl
Title: Re: refer to writable constant using different names
Post by: Thausand on June 18, 2022, 05:27:36 pm
Basically, I want to be able to sometimes refer to the "constant" SOME_TEXT using the name OTHER_TEXT but, everything I've tried so far makes the compiler unhappy.
Try var for read (no write, then error):

Code: Pascal  [Select][+][-]
  1. const
  2.   SOME_TEXT : pchar = 'some text';
  3. var
  4.   OTHER_TEXT: pchar absolute SOME_TEXT;
  5.  


Title: Re: refer to writable constant using different names
Post by: 440bx on June 18, 2022, 05:46:22 pm
Use a macro and be done with it.

MarkMLl
thank you for the suggestion but, I avoid macros like the plague.



Code: Pascal  [Select][+][-]
  1. const
  2.   SOME_TEXT : pchar = 'some text';
  3. var
  4.   OTHER_TEXT: pchar absolute SOME_TEXT;
  5.  
thank you, I appreciate the suggestion. 

Honestly, I was hoping for something that didn't use macros or "absolute" but, it's likely there is no other way.
Title: Re: refer to writable constant using different names
Post by: jamie on June 18, 2022, 06:00:47 pm
we don't have ORG here  :D

Title: Re: refer to writable constant using different names
Post by: Thaddy on June 18, 2022, 06:03:42 pm
Code: Pascal  [Select][+][-]
  1. program _Constants;
  2. {$MODE OBJFPC}
  3.  
  4. const
  5.   SOME_TEXT = 'some text';
  6.  
  7. const
  8.   OTHER_TEXT = SOME_TEXT;        
  9.  
  10. begin
  11. end.
Compiles.
Furthermore a string literal constant is assignment compatible to a PChar.
Unless you really want a writable const, I know of no other options.
That is because the writable consts have at declaration time different addresses, whereas a literal const occupies just has enough info for the compiler and no effective address.

Another option is an inlined function.
 
Title: Re: refer to writable constant using different names
Post by: 440bx on June 18, 2022, 06:14:07 pm
Furthermore a string literal constant is assignment compatible to a PChar.
Unless you really want a writable const, I know of no other options.
Thank you Thaddy.  I'll most likely use your suggestion. 

The problem with having the constant "untyped" (that is not specified as pchar) is that when using a mixture of "A" and "W" functions, FPC gives "preference" to the "A" version of the string even if it is being passed to a "W" function (which expects a unicode string), this causes the output to be something other than intended.  In those cases, getting the correct output requires explicitly typecasting the constant to "widechar" and, forgetting to do that is very easy.


Title: Re: refer to writable constant using different names
Post by: Thaddy on June 18, 2022, 06:49:09 pm
Partially, correct, but e.g. in mode delphiunicode string literals are Widechar. (AFAIK).
After the introduction of different string types the focus was on "compatibility" whereas I consider that a design error.
But, alas, other dialects  :D were first and the team adheres to as much compatibility that is reasonably possible.
TinyPortal © 2005-2018