On Mon, Jun 17, 2019 at 5:57 PM Ryan Joseph <generic...@gmail.com> wrote:

> The copy operator is always called on all assignments. Try actually
> running that and you’ll see. :)
>

Actually, after thinking about it some more, here's what I'd actually write
(in a perfect world where it was possible.)

program Test;

{$mode ObjFPC}
{$modeswitch AdvancedRecords}

type
  generic TList<T> = record
  public
    Data: array of T;
    procedure Assign(var Src: TList); inline;
    class operator Copy(constref Src: TList; var Dest: TList); //
apparently you cannot actually inline this...
    class function Create(const Num: Integer): TList; static; inline;
  end;

  procedure TList.Assign(var Src: TList);
  var Len: SizeInt;
  begin
    Len := Length(Src.Data);
    SetLength(Data, Len);
    Move(Src.Data[0], Data[0], SizeOf(T) * Len);
  end;

  class operator TList.Copy(constref Src: TList; var Dest: TList);
  begin
    // StaticAssert does not exist, unfortunately...
    StaticAssert(False, 'Don't use the := operator, call Assign instead!');
  end;

  class function TList.Create(const Num: Integer): TList;
  begin
    SetLength(Result.Data, Num);
  end;

begin
  // Do whatever here.
end.
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to