[fpc-devel] "class of TFoo".Create

Martin Frb lazarus at mfriebe.de
Sun Jul 6 16:33:50 CEST 2025


On 06/07/2025 16:17, Zoë Peterson via fpc-devel wrote:
>> I would personally call that unusual at best
>
> What do you two expect this construct to do that you think the current behavior is wrong? It’sobviously correct and expected to me as a fundamental  part of Object Pascal’s polymorphism. Constructing an object from a class reference is the entire reason that constructors are virtual. It’s how things like TPicture.Graphic are implemented to create the correct image loading object.
>

In the below, calling
   fc.Create;
is ok.

fc is a variable, and its value is a class. (that can be TFoo, or TFooChild)

Calling
   TFooClass.Create
on the otherhand...

TFooClass is not a class. Its a type for a variable that then as value 
can have a class.

And, it also seems to Statically translate into
   TFooClass.Create == TFoo.Create.

The type TFooClass itself can not be assigned a value. So it is always 
the same.


To answer your question: I expected it to be an error.
But if for cautious reasons that is not going to be, then at least a 
warning, nothing less



program Project1;
{$mode objfpc}
type TFoo = class end;
      TFooClass = class of TFoo;
var f: TFoo;
     fc: TFooClass;
begin
   fc := TFoo;
   f  := fc.Create;
   f  := TFooClass.Create;  // works
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20250706/022149b0/attachment.htm>


More information about the fpc-devel mailing list