[fpc-pascal] SetLength warnings - request

Jonas Maebe jonas at freepascal.org
Thu Jan 10 20:58:08 CET 2019

On 10/01/19 20:23, Martok wrote:
> Something I just found out, while changing some old code: why does that not work?
>    var
>      u: UnicodeString = nil;
>      // Incompatible types: got "Pointer" expected "UnicodeString"
>      a: AnsiString = nil;
>      // Illegal Expression
>      c: TCharArray = '';
>      // Syntax error, "(" expected but "const string" found
> Simply using '' as an initializer for Strings and nil for arrays solves it, so
> it's no big deal. I'm just curious why Strings do not have the niln typeconv

I'm fairly sure this was based on what Delphi allows and doesn't. Maybe 
it's because strings already had a primitive to initialise them to nil 
(and empty string) and arrays didn't (since array constants in code 
blocks did not exist at that time).

> and
> why the TCharArray can't be initialized (but assigned at runtime) with ''.

'' is parsed as a string constant, which means internally it becomes an 
array of char. The compiler does not support compile-time conversions of 
(constant or not) regular arrays to dynamic arrays.

> And
> why only the UnicodeString message actually relates what is wrong... ?

The CharArray error is also correct: array constants start with '('. I 
don't know why the ansistring error is less informative than the 
unicodestring one.


More information about the fpc-pascal mailing list