Duplicate identifier "$result" in generic function with nested function with result of type String
Compiler error Duplicate identifier "$result"
happens if a generic function has a nested function.
Nested function has to have a result type of String
(or AnsiString, WideString, UnicodeString, UTF8String, RawByteString or ShortString). This is not the case if the result type is Byte, TObject, Integer, PChar, etc.
Result of generic (outer) function can be of any type, it doesnt matter. Only nested function's result type does matter.
This fails to compile:
{$mode ObjFPC}{$H+}
generic function genericfunc<T>: String;
function innerfunc: String;
begin // project1.lpr(6,3) Error: Duplicate identifier "$result"
end;
begin
end;
begin
end.
<T>
This does not fail, it is generic but has missing {$mode ObjFPC}{$H+}
generic function genericfunc: String;
function innerfunc: String;
begin
end;
begin
end;
begin
end.
not String
:
This does not fail, the inner function result type is {$mode ObjFPC}{$H+}
generic function genericfunc<T>: String;
function innerfunc: Byte;
begin
end;
begin
end;
begin
end.
Temporary fix
{$modeswitch duplicatelocals}
, or {$mode Delphi}
as it have duplicatelocals
in its set of switches which makes possible to duplicate the local name result
.
Related issue
Related to #40104 (closed), but I found out it only fails if nested function has result type of String, thus a new issue report may be helpful.