[fpc-pascal] SetLength warnings - request
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?
> 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).
> 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.
> 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
More information about the fpc-pascal