[fpc-pascal] Dynamic array as result of class method?

Bo Berglund bo.berglund at gmail.com
Sun Nov 7 11:05:21 CET 2010


I am trying to design a buffer handling class where I want to have a
read function, which should return a result in the form of a byte
array of varying length. Something like this:

  TBufHandler = class
  private
    FBuf: array of byte;
    FBufSize: Cardinal;
    FWriteIndex: Cardinal;
    FReadIndex: Cardinal;
    function GetLength: Cardinal;
  public
    constructor Create(Size: Cardinal);
    destructor  Destroy; override;
    property DataLength: Cardinal read GetLength;
    function Write(Source: Pointer; Count: Cardinal): boolean;
    function Read(Dest: Pointer; Count: Cardinal): Cardinal; overload;
    function Read(Count: Cardinal): array of byte; overload;
  end;

Here I will create the FBuf dynamic array in the constructor and set
it to the length specified by the parameter Size.

The two overloaded Read methods both return data from the FBuf array
where the second one is supposed to work as follows:
- Check if there are Count bytes available from the ReadIndex to the
WriteIndex.
- Use the smaller of Count and that difference.
- Set the length of Result accordingly
- Copy/Move data from FBuf into Result

But when I use the Ctrl-Shift-C command on this to make Lazarus
complete the code I get an error on the second Read line:
sscomm.pas(40,37) Error: identifier expected, but array found

Is it illegal to specify a dynamic array as the result type of a
function?
If so why is it possible to use the string type as a function result,
a string is also dynamically sized inside the function??

Or is this a Lazarus implementation problem rather than a FPC problem?


Bo Berglund




More information about the fpc-pascal mailing list