From lazarus at mfriebe.de Sun Jul 6 13:51:21 2025 From: lazarus at mfriebe.de (Martin Frb) Date: Sun, 6 Jul 2025 13:51:21 +0200 Subject: [fpc-devel] "class of TFoo".Create Message-ID: <14c3fc8c-a8b3-47e7-a105-a963db8f484a@mfriebe.de> I stumbled (by accident) on (maybe) a curiosity TFooClass = class of TFoo; can store a class (that must be TFoo or subclass of it). I would have expected that the type TFooClass itself is not a class. (Its a container for a class / for lack of better wording) Yet TFooClass.Create compiles. And creates an instance of type TFoo. Is that intentional. 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. From bartjunk64 at gmail.com Sun Jul 6 14:38:47 2025 From: bartjunk64 at gmail.com (Bart) Date: Sun, 6 Jul 2025 14:38:47 +0200 Subject: [fpc-devel] "class of TFoo".Create In-Reply-To: <14c3fc8c-a8b3-47e7-a105-a963db8f484a@mfriebe.de> References: <14c3fc8c-a8b3-47e7-a105-a963db8f484a@mfriebe.de> Message-ID: On Sun, Jul 6, 2025 at 1:57?PM Martin Frb via fpc-devel wrote: > 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. Delphi (7) happily compiles that as well. And f is of type TFoo there as well. -- Bart From gareth at moreton-family.com Sun Jul 6 15:11:58 2025 From: gareth at moreton-family.com (J. Gareth Moreton) Date: Sun, 6 Jul 2025 14:11:58 +0100 Subject: [fpc-devel] "class of TFoo".Create In-Reply-To: References: <14c3fc8c-a8b3-47e7-a105-a963db8f484a@mfriebe.de> Message-ID: <399c0ad6-dfb0-4f32-a685-3d29eb576060@moreton-family.com> I would personally call that unusual at best.? Logically I would consider it a type mismatch or a syntax error of some kind, since TFoo is a class and TFooClass is a metaclass.? Also, if I saw such code in a project, I would think someone made a mistake that didn't get caught, since TFoo.Create would make much more sense. I'd wonder if, under OBJFPC mode, such a construct should raise a compiler error because I honestly can't see a situation where it would be correct and intentional over using the associated class in its place. Kit On 06/07/2025 13:38, Bart via fpc-devel wrote: > On Sun, Jul 6, 2025 at 1:57?PM Martin Frb via fpc-devel > wrote: > > >> 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. > Delphi (7) happily compiles that as well. > And f is of type TFoo there as well. > -- This email has been checked for viruses by Avast antivirus software. www.avast.com From zoe at scootersoftware.com Sun Jul 6 16:17:42 2025 From: zoe at scootersoftware.com (=?utf-8?Q?Zo=C3=AB_Peterson?=) Date: Sun, 6 Jul 2025 17:17:42 +0300 Subject: [fpc-devel] "class of TFoo".Create In-Reply-To: <399c0ad6-dfb0-4f32-a685-3d29eb576060@moreton-family.com> References: <399c0ad6-dfb0-4f32-a685-3d29eb576060@moreton-family.com> Message-ID: > ?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. Zo? Peterson Scooter Software From gareth at moreton-family.com Sun Jul 6 16:31:05 2025 From: gareth at moreton-family.com (J. Gareth Moreton) Date: Sun, 6 Jul 2025 15:31:05 +0100 Subject: [fpc-devel] "class of TFoo".Create In-Reply-To: References: <399c0ad6-dfb0-4f32-a685-3d29eb576060@moreton-family.com> Message-ID: <804965b0-3f9c-4bf9-8a30-8d792622d922@moreton-family.com> What I mean is that if you want to instantiate an object of class TFoo, you'd call TFoo.Create, not TFooClass.Create. If you want to take advantage of Object Pascal's unique polymorphism and instantiate a descendant class of TFoo, you'd call MetaClassVariable.Create rather than TFooClass.Create, where MetaClassVariable is defined somewhere as "var MetaClassVariable: TFooClass;" and then set it accordingly before the instantiation. To just call TFooClass.Create, and TFooClass is implicitly converted to TFoo, feels a bit 'off' and is asking for trouble, while seeing "TFooClass.Create" in the code would be indictive of a bug in my opinion, since the programmer either meant "TFoo" or a variable of type TFooClass instead of "TFooClass" itself. On 06/07/2025 15: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. > > Zo? Peterson > Scooter Software > > > _______________________________________________ > fpc-devel maillist - fpc-devel at lists.freepascal.org > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel > -- This email has been checked for viruses by Avast antivirus software. www.avast.com From lazarus at mfriebe.de Sun Jul 6 16:33:50 2025 From: lazarus at mfriebe.de (Martin Frb) Date: Sun, 6 Jul 2025 16:33:50 +0200 Subject: [fpc-devel] "class of TFoo".Create In-Reply-To: References: <399c0ad6-dfb0-4f32-a685-3d29eb576060@moreton-family.com> Message-ID: <5a8e99eb-9aa5-4354-9687-499c5c9fb7d1@mfriebe.de> 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: From fpc at pascalprogramming.org Sun Jul 6 16:41:53 2025 From: fpc at pascalprogramming.org (Marco van de Voort) Date: Sun, 6 Jul 2025 16:41:53 +0200 Subject: [fpc-devel] "class of TFoo".Create In-Reply-To: <804965b0-3f9c-4bf9-8a30-8d792622d922@moreton-family.com> References: <399c0ad6-dfb0-4f32-a685-3d29eb576060@moreton-family.com> <804965b0-3f9c-4bf9-8a30-8d792622d922@moreton-family.com> Message-ID: <48168436-691d-4666-ba3d-091c6bc97251@pascalprogramming.org> Op 6-7-2025 om 16:31 schreef J. Gareth Moreton via fpc-devel: > What I mean is that if you want to instantiate an object of class > TFoo, you'd call TFoo.Create, not TFooClass.Create. I also don't see the real value of that "feature", but iirc it was added since Delphi allows it. From gareth at moreton-family.com Sun Jul 6 16:52:15 2025 From: gareth at moreton-family.com (J. Gareth Moreton) Date: Sun, 6 Jul 2025 15:52:15 +0100 Subject: [fpc-devel] "class of TFoo".Create In-Reply-To: <48168436-691d-4666-ba3d-091c6bc97251@pascalprogramming.org> References: <399c0ad6-dfb0-4f32-a685-3d29eb576060@moreton-family.com> <804965b0-3f9c-4bf9-8a30-8d792622d922@moreton-family.com> <48168436-691d-4666-ba3d-091c6bc97251@pascalprogramming.org> Message-ID: Going by what Martin just said, should this throw a warning, at least if not in Delphi mode?? Or an error?? This "feature" being used is more likely due to a programming bug and I feel that the developer's attention should be drawn to it so it can be replaced with TFoo.Create if that's what they meant, or a metaclass variable. Kit On 06/07/2025 15:41, Marco van de Voort via fpc-devel wrote: > > Op 6-7-2025 om 16:31 schreef J. Gareth Moreton via fpc-devel: >> What I mean is that if you want to instantiate an object of class >> TFoo, you'd call TFoo.Create, not TFooClass.Create. > > I also don't see the real value of that "feature", but iirc it was > added since Delphi allows it. > > > _______________________________________________ > fpc-devel maillist? -? fpc-devel at lists.freepascal.org > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel > -- This email has been checked for viruses by Avast antivirus software. www.avast.com