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

Upvotes: 5

Views: 93

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

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;

Upvotes: 6

Related Questions