Reputation: 11608
I have the following function which is supposed to split a string into a string array (I'm using Geany IDE and fpc compiler):
function Split(const str: string; const separator: string): array of string;
var
i, n: integer;
strline, strfield: string;
begin
n:= Occurs(str, separator);
SetLength(Result, n + 1);
i := 0;
strline:= str;
repeat
if Pos(separator, strline) > 0 then
begin
strfield:= Copy(strline, 1, Pos(separator, strline) - 1);
strline:= Copy(strline, Pos(separator, strline) + 1,
Length(strline) - pos(separator,strline));
end
else
begin
strfield:= strline;
strline:= '';
end;
Result[i]:= strfield;
Inc(i);
until strline= '';
if Result[High(Result)] = '' then SetLength(Result, Length(Result) -1);
end;
The compiler reports an error:
calc.pas(24,61) Error: Type identifier expected
calc.pas(24,61) Fatal: Syntax error, ";" expected but "ARRAY" found
As far as I can see the syntax is correct, what is the problem here?
Upvotes: 1
Views: 4473
Reputation: 54802
Compiler is telling you that you cannot return an untyped dynamic array. You can declare f.i.
type TStringArray = array of string;
and you can return a TStringArray
from the function. Note that, a variable declared as TStringArray
will not be compatible with a similarly declared but differently typed array, for instance with type TOtherStringArray = array of string
.
Upvotes: 3