[fpc-pascal] Abstract classes ignored
Sven Barth
pascaldragon at googlemail.com
Sat Apr 17 23:09:12 CEST 2021
Am 17.04.2021 um 20:30 schrieb Ryan Joseph via fpc-pascal:
> 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
More information about the fpc-pascal
mailing list