Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-04

Abstract classes ignored

16 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

From the documentation: "An abstract class is a class that cannot be instantiated directly. Instead, a descendent class must always be instantiated. However, for Delphi compatibility, the compiler ignores this directive."

Why does this get ignored and what does this have to do with Delphi? I personally would like for this to actually work and give a compiler error if you instantiate the abstract class.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote
From the documentation: "An abstract class is a class that cannot be instantiated directly. Instead, a descendent class must always be instantiated. However, for Delphi compatibility, the compiler ignores this directive." Why does this get ignored and what does this have to do with Delphi? I personally would like for this to actually work and give a compiler error if you instantiate the abstract class.

The compiler will generate a warning in case you instantiate a class that is abstract or has abstract methods. You can escalate these warnings to errors if you need:

=== code begin ===

program tabstract;
{$MODE OBJFPC}
{$WARN CONSTRUCTING_ABSTRACT ERROR}
type
   TAbstract1 = class abstract
   end;
   TAbstract2 = class
     procedure Test; virtual; abstract;
   end;
var
   a1: TAbstract1;
   a2: TAbstract2;
begin
   a1 := TAbstract1.Create;
   a2 := TAbstract2.Create;
end.

=== code end ===

This will generate two (different) errors.

With this we're allowing more than Delphi does (which warns in the second case, but the warning can not be escalated to error and does not warn for the first case at all), but Delphi compatbility is more important here, thus the default is a warning and not an error. This will not be changed.

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
On Apr 17, 2021, at 3:09 PM, Sven Barth wrote: The compiler will generate a warning in case you instantiate a class that is abstract or has abstract methods. You can escalate these warnings to errors if you need:

It's probably not practical to put that warning into all potential files but a warning is still probably good enough as a reminder. It's curious though why it's not an error by default, because if the class is abstract and you instantiate it and try to use it things are going to fail anyways so why does the compiler allow it in the first place?

Regards,
Ryan Joseph

Reply by email

C Western
to Ryan Joseph
Ryan Joseph wrote 12 lines
> On Apr 17, 2021, at 3:09 PM, Sven Barth wrote: > > The compiler will generate a warning in case you instantiate a class that is abstract or has abstract methods. You can escalate these warnings to errors if you need: It's probably not practical to put that warning into all potential files but a warning is still probably good enough as a reminder. It's curious though why it's not an error by default, because if the class is abstract and you instantiate it and try to use it things are going to fail anyways so why does the compiler allow it in the first place? Regards, Ryan Joseph _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

My own code will have cases where I have only implemented some of the abstract methods for some classes, in principle because the code is incomplete, but there are also case where it never makes sense to implement them and calling them indicates a logical error elsewhere in the code.

Colin

Reply by email

Sven Barth
to Ryan Joseph: It's probably not practical to put that warning into all potential files but a warning is…
Ryan Joseph wrote 5 lines
> On Apr 17, 2021, at 3:09 PM, Sven Barth wrote: > > The compiler will generate a warning in case you instantiate a class that is abstract or has abstract methods. You can escalate these warnings to errors if you need: It's probably not practical to put that warning into all potential files but a warning is still probably good enough as a reminder. It's curious though why it's not an error by default, because if the class is abstract and you instantiate it and try to use it things are going to fail anyways so why does the compiler allow it in the first place?

As I have said: Delphi compatibility.

Regards,
Sven

Reply by email

Graeme Geldenhuys
to Sven Barth: The compiler will generate a warning in case you instantiate a class that is abstract or…
Sven Barth wrote
but Delphi compatbility is more important here, thus the default is a warning and not an error.

How is this beneficial? The compiler should help the developer, but here it was decided that it's beneficial for the program to crash at runtime, because follow the broken Delphi compiler is more important! *shrug*

Such decisions really make me worry about the future of FPC. It ends up being NO better than Delphi, so why bother using FPC then. Stick to Delphi instead.

Regards,
Graeme

Reply by email

Jonas Maebe
to Graeme Geldenhuys
Graeme Geldenhuys wrote 5 lines
On 17/04/2021 10:09 pm, Sven Barth via fpc-pascal wrote: > but Delphi compatbility is more > important here, thus the default is a warning and not an error. How is this beneficial? The compiler should help the developer,

People who want to compile code in both FPC and Delphi are developers too. People who want to use their knowledge of Delphi to program in FPC are developers too.

You can never make everyone happy, and sometimes we go for incompatibility by default, and sometimes we don't. In this case, you can easily make it an error yourself with {$warn 4046 error}.

Such decisions really make me worry about the future of FPC. It ends up being NO better than Delphi, so why bother using FPC then. Stick to Delphi instead.

It's not like this is a new policy that you just discovered, so please stop already with the flamebait drama about how this makes you worry about the future of FPC.

Jonas

Reply by email

Travis Siegel
to Graeme Geldenhuys: How is this beneficial? The compiler should help the developer, but here it was decided…
Graeme Geldenhuys wrote 10 lines
On 17/04/2021 10:09 pm, Sven Barth via fpc-pascal wrote: > but Delphi compatbility is more > important here, thus the default is a warning and not an error. How is this beneficial? The compiler should help the developer, but here it was decided that it's beneficial for the program to crash at runtime, because follow the broken Delphi compiler is more important! *shrug* Such decisions really make me worry about the future of FPC. It ends up being NO better than Delphi, so why bother using FPC then. Stick to Delphi instead.

If you're willing to pay for me to obtain a delphi license, then I'd happily switch.  Seeing as how they charge over a thousand dollars even during their supposed sales, I have no interest in spending that kind of money for an environment that FPC offers for free. That means I don't have to spend my limited income on a license for a program I use a few times a year.

Admittedly, part of the reason I use it so rarely is because I really need to dig into it's gui and tcp capabilities (I program from the ocmmand prompt, but being able to generate gui screens on the fly would be nice).  I know Delphi can do this, but the last time I asked them about pricing, they told me it'd cost $1000 for just delphi 7, and that only costed $995 when it first came out, nearly 20 years ago.  I refuse to support that kind of price gouging, even if I could afford it.

But, as I said, if you're willing to foot the bill, I'd happily use it, as it does indeed have (some) advantages.

Reply by email

Bart
to Travis Siegel: If you're willing to pay for me to obtain a delphi license, then I'd happily switch. …
PM Travis Siegel wrote
... but the last time I asked them about pricing, they told me it'd cost $1000 for just delphi 7, and that only costed $995 when it first came out, nearly 20 years ago.

Wow!
Long time ago, I bought Delphi2 (it was bundled with Delphi 1, and that was the one I needed, since I was on Win 3.10) for about ƒ200. (€1.00 = ƒ2.20).
It came with a full manual (on paper that is).

Those were the days....

Bart

Reply by email

Sven Barth
to Jonas Maebe

Jonas Maebe via fpc-pascal schrieb am
So., 18. Apr. 2021, 21:19:

Show quoted text 7 lines
On 18/04/2021 18:08, Jonas Maebe via fpc-pascal wrote: > In this case, you > can easily make it an error yourself with {$warn 4046 error}. Correction: that's make it an error to construct a classes that contain abstract methods. For abstract classes, it's {$warn 4122 error}

Or as I had written already: {$WARN CONSTRUCTING_ABSTRACT ERROR}. This will switch both warnings to error.

Regards,
Sven

Reply by email

Sven Barth
to Graeme Geldenhuys: So do the logical thing... Throw an exception! Don't promote a faulty compiler. Fix your…

Graeme Geldenhuys via fpc-pascal schrieb
am So., 18. Apr. 2021, 17:54:

Show quoted text 9 lines
On 18/04/2021 11:31 am, C Western via fpc-pascal wrote: > but there are also case where it never makes sense to > implement them and calling them indicates a logical error elsewhere in > the code. So do the logical thing... Throw an exception! Don't promote a faulty compiler. Fix your code instead.

Calling an abstract method will already result in an EAbstractError to be thrown.

Regards,
Sven

Reply by email