Forum > General

refer to writable constant using different names

<< < (2/2)

Thaddy:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program _Constants;{$MODE OBJFPC} const  SOME_TEXT = 'some text'; const  OTHER_TEXT = SOME_TEXT;          beginend.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.
 

440bx:

--- Quote from: Thaddy on June 18, 2022, 06:03:42 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.

--- End quote ---
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.


Thaddy:
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.

Navigation

[0] Message Index

[*] Previous page

Go to full version