Recent

Author Topic: refer to writable constant using different names  (Read 824 times)

440bx

  • Hero Member
  • *****
  • Posts: 3946
refer to writable constant using different names
« 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)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: refer to writable constant using different names
« Reply #1 on: June 18, 2022, 05:16:24 pm »
Use a macro and be done with it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: refer to writable constant using different names
« Reply #2 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.  



440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: refer to writable constant using different names
« Reply #3 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: refer to writable constant using different names
« Reply #4 on: June 18, 2022, 06:00:47 pm »
we don't have ORG here  :D

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: refer to writable constant using different names
« Reply #5 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.
 
« Last Edit: June 18, 2022, 06:08:17 pm by Thaddy »
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: refer to writable constant using different names
« Reply #6 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.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: refer to writable constant using different names
« Reply #7 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.
« Last Edit: June 18, 2022, 06:51:22 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018