[fpc-pascal] Default type of string literals
Bernd
prof7bit at gmail.com
Mon Jan 4 12:35:53 CET 2016
Consider the following program:
program stringtypeoverloadproblem;
{$mode objfpc}{$H+}
uses
sysutils;
var
Foo: String;
begin
Foo := ConcatPaths(['foo', 'bar']);
end.
With fpc 3.0 this will fail to compile with the error:
Hint: (11030) Start of reading config file
C:\lazarus\fpc\3.0.0\bin\i386-win32\fpc.cfg
Hint: (11031) End of reading config file
C:\lazarus\fpc\3.0.0\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 3.0.0 [2015/12/07] for i386
Copyright (c) 1993-2015 by Florian Klaempfl and others
(1002) Target OS: Win32 for i386
(3104) Compiling stringtypeoverloadproblem.lpr
C:\Users\xec3\workspace\testus\stringtypeoverloadproblem.lpr(12,10)
Error: (4057) Can't determine which overloaded function to call
Hint: (5039) Found declaration: ConcatPaths(const {Open} Array Of
RawByteString):AnsiString;
Hint: (5039) Found declaration: ConcatPaths(const {Open} Array Of
UnicodeString):UnicodeString;
stringtypeoverloadproblem.lpr(15) Fatal: (10026) There were 1 errors
compiling module, stopping
Fatal: (1018) Compilation aborted
Error: C:\lazarus\fpc\3.0.0\bin\i386-win32\ppc386.exe returned an error exitcode
The reason is it seems it cannot decide which type of strings should
be used for passing to the function. The workaround is to explicitely
cast one of them to a certain string type first.
Is this intentional that a string litereal does not default to a
certain type (for example the same type that the Type "String"
defaults to, in this case with the $H+ switch this would be
AnsiString?
Is there maybe another compiler directive for this (which I did not
find but I may have overlooked it) or maybe should such a directive be
created if it does not exist yet?
The problem has occurred when updating to the new Lazarus release
candidate wich comes with 3.0 and some older code of mine failed to
compile, now I have fixed it by explicitely assigning the type
AnsiString to some of my previously untyped string constants. This
mabe me wonder why
const
FOO_DIR = 'foobar';
would not just simply make an AnsiString constant by default, even
when not explicitely writing
const
FOO_DIR: AnsiString = 'foobar';
-Bernd
More information about the fpc-pascal
mailing list