#1 2017-11-22 12:11:44

wloochacz
Member
Registered: 2015-01-03
Posts: 45

Use of TObjectDynArrayWrapper and compact the wrapper array...

Hello!

If I wrote the code (Delphi XE and actual mORMot) according to the documentation, i.e.:

var
  DA : IObjectDynArray; // defined BEFORE the dynamic array itself
  A  : array of TOA;
  i  : integer;
begin
  DA := TObjectDynArrayWrapper.Create(A);

  for I := 1 to 10 do
    DA.Add(TOA.Create(IntToStr(I)));
end;

I'm getting an exception: You MUST define your IObjectDynArray field BEFORE the corresponding dynamic array.

If I wrote:

var
  A  : array of TOA;
  DA : IObjectDynArray; // defined BEFORE the dynamic array itself
  i  : integer;
begin
  DA := TObjectDynArrayWrapper.Create(A);

  for I := 1 to 10 do
    DA.Add(TOA.Create(IntToStr(I)));
end;

Everything is OK.
This is a bug in manual or what?

In addition, please add the Compact method to DynArrayWrapper. Something like that:

  IObjectDynArrayEx = interface(IObjectDynArray)
  ['{FD8E284A-9585-4FE8-96CB-0FB98350731C}']
    procedure Compact;
  end;

  TObjectDynArrayWrapperEx = class(TObjectDynArrayWrapper, IObjectDynArrayEx)
  public
    procedure Compact;
  end;

implementation

{ TObjectDynArrayWrapperEx }

procedure TObjectDynArrayWrapperEx.Compact;
begin
  SetLength(TObjectDynArray(fValue^), Count);
end;

Offline

#2 2017-11-22 13:56:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Use of TObjectDynArrayWrapper and compact the wrapper array...

The notion of "before" is relative to the instance creation time.
It is not "before" in the list of stack-allocated variable, it is "before" the actual allocation.

Offline

#3 2017-11-22 14:17:24

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Use of TObjectDynArrayWrapper and compact the wrapper array...

I've just introduced TObjectDynArrayWrapper.Slice as you requested.

See https://synopse.info/fossil/info/168cab3528

Offline

#4 2017-11-22 17:32:14

wloochacz
Member
Registered: 2015-01-03
Posts: 45

Re: Use of TObjectDynArrayWrapper and compact the wrapper array...

ab wrote:

I've just introduced TObjectDynArrayWrapper.Slice as you requested.

See https://synopse.info/fossil/info/168cab3528

Thank you!

BTW - Slice?
Are you sure it's the right name? ;-)
In my opinion, it's more about compacting this array than slicing it...

Last edited by wloochacz (2017-11-22 20:23:47)

Offline

#5 2017-11-23 04:05:41

Chaa
Member
Registered: 2011-03-26
Posts: 244

Re: Use of TObjectDynArrayWrapper and compact the wrapper array...

Offline

#6 2017-11-23 07:35:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Use of TObjectDynArrayWrapper and compact the wrapper array...

And compact is about removing gaps between items...

There is no perfect wording.

Offline

#7 2017-11-23 15:30:07

Junior/RO
Member
Registered: 2011-05-13
Posts: 207

Re: Use of TObjectDynArrayWrapper and compact the wrapper array...

About naming: Ruby uses Array.Compact() for this function.

Last edited by Junior/RO (2017-11-23 15:30:32)

Offline

#8 2017-11-24 17:54:10

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Use of TObjectDynArrayWrapper and compact the wrapper array...

Maybe Array.Trim()?

Offline

Board footer

Powered by FluxBB