delphianonymous-types

Anonymous Record constructors in delphi


Is it possible to use records as a method parameter, and call it without implicitly declaring an instance of said record?

I would like to be able to write code like this.

type
  TRRec = record
    ident : string;
    classtype : TClass;
  end;

procedure Foo(AClasses : array of TRRec);

then calling the method like this or something similar.

Foo([('Button1', TButton), ('Lable1', TLabel)]);

I'm still stuck on Delphi 5 by the way.


Solution

  • Yes. Almost.

    type
      TRRec = record
        ident : string;
        classtype : TClass;
      end;
    
    function r(i: string; c: TClass): TRRec;
    begin
      result.ident     := i;
      result.classtype := c;
    end;
    
    procedure Foo(AClasses : array of TRRec);
    begin
      ;
    end;
    
    // ...
    Foo([r('Button1', TButton), r('Lable1', TLabel)]);