Sign in Join

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

Mailing lists / fpc-pascal / 2009-08

how to simulate abstract properties

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Marc Santhoff

Hi,

I'm searching for a way to have the compiler handle properties declared inside an abstract base class as if they were methods.

With a declaration like this:

TBase = class
private
  function getA: integer; virtual; abstract;
public
  property A:integer read getA;
end;
TDerived = class(TBase)
private
  function getA: integer; override;
end;
TDerived2 = class(TBase)
private
end;

the compiler runs and throws no error. Fine, but this way every class derived from TBase is in danger of calling an abstract method at runtime. If at an instance of class TDerived2 the property A is read a runtime error 211 is thrown.

What I want is to ensure not forgetting to override any method. The compiler should stop on error when this happens.
How can this be done?

(I know the compiler does emit a warning, but switching to "stop on warning" is o option for me.)

TIA,

Marc Santhoff

Reply by email

Marc Santhoff
to Jonas Maebe
Jonas Maebe wrote 11 lines
On 16 Aug 2009, at 04:37, Marc Santhoff wrote: > What I want is to ensure not forgetting to override any method. The > compiler should stop on error when this happens. > How can this be done? > > (I know the compiler does emit a warning, but switching to "stop on > warning" is o option for me.) Seems like there is a practical use-case for http://bugs.freepascal.org/view.php?id=13957 after all :)

Do I understand correctly that this patch allow me to remap the warning message from the compiler to be evaluated like an error? That would do it ...

Next question will be which compiler version from the releases I have to choose to make use of this feature?

Thanks,

Marc Santhoff

Reply by email

Jonas Maebe
to Marc Santhoff
Marc Santhoff wrote 10 lines
Am Sonntag, den 16.08.2009, 12:00 +0200 schrieb Jonas Maebe: > > Seems like there is a practical use-case for http://bugs.freepascal.org/view.php?id=13957 > after all :) Do I understand correctly that this patch allow me to remap the warning message from the compiler to be evaluated like an error? That would do it ...

Yes.

Next question will be which compiler version from the releases I have to choose to make use of this feature?

None, because the patch hasn't been applied yet.

Jonas

Reply by email

Marc Santhoff
to Jonas Maebe
Jonas Maebe wrote 14 lines
On 16 Aug 2009, at 19:58, Marc Santhoff wrote: > Am Sonntag, den 16.08.2009, 12:00 +0200 schrieb Jonas Maebe: >> > >> Seems like there is a practical use-case for http://bugs.freepascal.org/view.php?id=13957 >> after all :) > > Do I understand correctly that this patch allow me to remap the > warning > message from the compiler to be evaluated like an error? That would do > it ... Yes.

Very nice. :)

Show quoted text 5 lines
> Next question will be which compiler version from the releases I > have to > choose to make use of this feature? None, because the patch hasn't been applied yet.

I was too impatient, after reading the complete issue history I know that. ;)

I'll have to patch and build the compiler myself then. I guess that checking out a recent 2.3.x version and reading the build faq would be the next step?

Another solution to my problem would be to have virtual abstract properties, don't know if that would make sense and be more generally usable ...
It would introduce the need to replicate the property declaration with override modifier, more typing work.
Has anyone else had the need to use properties this way?

Marc Santhoff

Reply by email

Graeme Geldenhuys
to Marc Santhoff: I'm searching for a way to have the compiler handle properties declared inside an…
Marc Santhoff wrote
the compiler runs and throws no error. Fine, but this way every class

Because you did not instantiate any instances of that class. Here is the output after compiling my sample program with FPC 2.2.5

===============================
$ fpc test1.pas
Free Pascal Compiler version 2.2.5 [2009/05/28] for i386 Copyright (c) 1993-2008 by Florian Klaempfl
Target OS: Linux for i386
Compiling test1.pas
test1.pas(31,24) Warning: Constructing a class "TDerived2" with abstract methods
Linking test1
35 lines compiled, 0.1 sec
1 warning(s) issued
===============================

It clearly shows the error, which is correct. Just declaring a class with abstract methods doesn't mean the compiler must throw an error. TDerived2 might have another class deriving from it, which adds the getA implementation.

As far as I can see, the compiler is doing everything correctly.

===========[ complete sample application ]==================
program test1;
{$mode objfpc}{$H+}
type
    TBase = class(TObject)
    private
      function getA: integer; virtual; abstract;
    public
      property A:integer read getA;
    end;
    TDerived = class(TBase)
    private
      function getA: integer; override;
    end;
    TDerived2 = class(TBase)
    end;
function TDerived.getA: integer;
begin
   result := 99;
end;
var
   a: TDerived;
   b: TDerived2;
begin
   a := TDerived.Create;
   b := TDerived2.Create;
   a.Free;
   b.Free;
end.
==================[ end ]=======================

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Marc Santhoff
to Graeme Geldenhuys
Graeme Geldenhuys wrote 6 lines
Marc Santhoff wrote: > > the compiler runs and throws no error. Fine, but this way every class Because you did not instantiate any instances of that class. Here is the output after compiling my sample program with FPC 2.2.5

I did while testing.

Show quoted text 17 lines
=============================== $ fpc test1.pas Free Pascal Compiler version 2.2.5 [2009/05/28] for i386 Copyright (c) 1993-2008 by Florian Klaempfl Target OS: Linux for i386 Compiling test1.pas test1.pas(31,24) Warning: Constructing a class "TDerived2" with abstract methods Linking test1 35 lines compiled, 0.1 sec 1 warning(s) issued =============================== It clearly shows the error, which is correct. Just declaring a class with abstract methods doesn't mean the compiler must throw an error. TDerived2 might have another class deriving from it, which adds the getA implementation.

No, it shows a warning message, no error.

The point is, the compiler finishes making a runnable program. I personally don't want that to happen because it will make avoidable this compiled binary get's delivered somehow
(e.g. by error or ignorance) it will be a nice source of support calls or even more dangerous consequences.
Making the compiler stop on error will prevent such trouble.

As far as I can see, the compiler is doing everything correctly.

Don't get me wrong, I'm not saying the compiler does sth. wrong, I only want to work it differently in classifying some warnings as an error. The patch Jonas showed does exactly that, so I'm happy.

Marc Santhoff

Reply by email

Graeme Geldenhuys
to Marc Santhoff
Marc Santhoff wrote
No, it shows a warning message, no error. The point is, the compiler finishes making a runnable program. I personally don't want that to happen because it will make avoidable

OK, I now see what you mean. Luckily I pay close attention to any Warnings that the compiler gives. Hints and Notes at a lesser degree.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email