Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Metaclass default argument value (Delphi 2009)

I want to give a default value in a metaclass argument:

type
  TMyClass = class
  end;

type
  TMyClassMetaClass = class of TMyClass;

procedure MyProcedure(const AMetaClass: TMyClassMetaClass = TMyClass);

It is possible? In Delphi2009 it gives me the error: E2026 Constant expression expected

like image 855
Jonathan Olavarría Avatar asked Jun 26 '15 19:06

Jonathan Olavarría


Video Answer


1 Answers

According to the rules of the language, a metaclass is not a constant expression. So your best bet is to use overloading instead:

procedure MyProcedure(const AMetaClass: TMyClassMetaClass); overload;
procedure MyProcedure; overload;

And in the implementation:

procedure MyProcedure(const AMetaClass: TMyClassMetaClass);
begin
  ....
end;  

procedure MyProcedure;
begin
  MyProcedure(TMyClass);
end;
like image 186
David Heffernan Avatar answered Oct 27 '22 10:10

David Heffernan