Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-11

Dynamic array as result of class method?

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

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

Jonas Maebe
to Bo Berglund
Bo Berglund wrote 9 lines
function Read(Count: Cardinal): array of byte; overload; end; 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?

No, but you cannot declare it inline, just like you cannot have

function read(count: cardinal): record b: byte end;

Use a separately defined type:

type
  tbytearray = array of byte;

Jonas

Wimpie Nortje
to Jonas Maebe

TByteDynArray is defined in unit 'types'

Jonas Maebe wrote 28 lines
On 07 Nov 2010, at 11:05, Bo Berglund wrote: > function Read(Count: Cardinal): array of byte; overload; > end; > > 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? No, but you cannot declare it inline, just like you cannot have function read(count: cardinal): record b: byte end; Use a separately defined type: type tbytearray = array of byte; Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal