Sign in Join

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

Mailing lists / fpc-pascal / 2016-06

Type helper for array

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Vojtěch Čihák

Hello,
 
I've got following declaration:
...
{$modeswitch typehelpers}
 
interface
 
uses
  Classes, SysUtils;
 
type
  TBuffer = array of Single;
 
  { TBufferHelper }
  TBufferHelper = type helper for TBuffer
    procedure Clear;
  end;
...
var aBuffer: TBuffer;
...
And I can do:
aBuffer[3]:=5;
aBuffer.Clear;
 
Is it possible to do this without helper? Since it's IMO silly to have helper in the same unit as the original declaration. Helpers are usually used where extending of the original type is not possible, aren't they?  
Thanks,  V.

Sven Barth
to Vojtěch Čihák
"Vojtěch Čihák" wrote 27 lines
Hello, I've got following declaration: ... {$modeswitch typehelpers} interface uses Classes, SysUtils; type TBuffer = array of Single; { TBufferHelper } TBufferHelper = type helper for TBuffer procedure Clear; end; ... var aBuffer: TBuffer; ... And I can do: aBuffer[3]:=5; aBuffer.Clear; Is it possible to do this without helper? Since it's IMO silly to have

helper in the same unit as the original declaration. Helpers are usually used where extending of the original type is not possible, aren't they?

You can always set the array to Nil or use FillChar(), depending on what you want to achieve with Clear. You can also put the helper in another unit if you want, it doesn't need to be in the same as the type it extends.

Regards,
Sven

Jürgen Hestermann
to Sven Barth
Sven Barth wrote 5 lines
Am 09.06.2016 17:57 schrieb "Vojtech Cihák" : > aBuffer.Clear; > Is it possible to do this without helper? You can always set the array to Nil

IMO SetLength(aBuffer,0) would the most natural way to clear the array.

Sven Barth
to Jürgen Hestermann
"Jürgen Hestermann" wrote 10 lines
Am 2016-06-09 um 20:18 schrieb Sven Barth: > Am 09.06.2016 17:57 schrieb "Vojtech Cihák" : > > aBuffer.Clear; > > Is it possible to do this without helper? > > You can always set the array to Nil IMO SetLength(aBuffer,0) would the most natural way to clear the array.

*shrugs* That's also a way. Bit the question is whether he want to set the array to empty or if he wants to clear the array's contents without changing the size.

Regards,
Sven

Vojtěch Čihák
to Jürgen Hestermann: IMO SetLength(aBuffer,0) would the most natural way to clear the array.

@ But the question is whether he want to set the array to empty or if he wants to clear the array's contents without changing the size.  
The second is right but it was not the point. "Clear" is just one example of several methods I want to encalupsate into TBuffer. I decided use the type helper now. Another solution is class:  
TBuffer = class
  Data: array of Single;
  procedure Create(ASize: Integer);
  procedure Clear;
end;
 
So I would call aBuffer.Data[3]:=5; instead of aBuffer[3]:=5;. Thanks
______________________________________________________________

Show quoted text 5 lines
Od: Sven Barth Komu: FPC-Pascal users discussions Datum: 10.06.2016 08:03 Předmět: Re: [fpc-pascal] Type helper for array
"Jürgen Hestermann" < > wrote 10 lines
Am 2016-06-09 um 20:18 schrieb Sven Barth: > Am 09.06.2016 17:57 schrieb "Vojtech Cihák" < >: > > aBuffer.Clear; > > Is it possible to do this without helper? > > You can always set the array to Nil IMO SetLength(aBuffer,0) would the most natural way to clear the array.

*shrugs* That's also a way. Bit the question is whether he want to set the array to empty or if he wants to clear the array's contents without changing the size. Regards,
Sven

----------