Free Pascal => FPC development => Topic started by: jwharton on November 24, 2015, 06:34:15 pm

Title: Static Method implemented by a sub-class
Post by: jwharton on November 24, 2015, 06:34:15 pm
I had an idea for a new compiler/language feature that should be feasible... Hopefully I can explain it well enough to make sense...

I am in the process of writing a driver layer for my components IB Objects so that I can use it with other databases besides InterBase and Firebird. I have made a lot of progress so far and while doing this I came across an idea that I believe would help performance. But, it seems the compiler isn't able to do what I would like.

I would like the ability to have a base class to lay down all of the basic calls the driver requires and then to have one, and only one, sub-class implement them. I would like the performance of each sub-class to have the same performance as if those methods were all static methods. For now, I am having to use virtual abstract methods in the base class and then override them in the sub-class. What I would like to see is a way to have methods be declared as abstract without being virtual at the same time. This would impose the limitation that any sub-class would be required to implement them and that they would be implemented as static methods. This would also impose the limitation that only those sub-classes (or their descendants) could be instantiated.

Hopefully what I have stated here is clear enough to know what I am getting at. I suppose this would affect the language in such a way that you could declare a method as ABSTRACT without the VIRTUAL designation.

How much of a performance loss will the virtual method layer actually impose? Perhaps it isn't significant enough to even worry about?

Thanks for any feedback.
Title: Re: Static Method implemented by a sub-class
Post by: marcov on November 24, 2015, 07:57:54 pm
The problem is that you specify what you want, not how you want to achieve it.

The trouble is that the compiler must be able to generate code with calls for the base class, while with only the base class it is not known what addresses to call.

Specifying how a compiler handles a planned feature is an occam's razor for feasability. This is why the faq for new compiler extensions recommends to write a report for it including it:

Title: Re: Static Method implemented by a sub-class
Post by: Jonas Maebe on November 25, 2015, 01:33:17 pm
The overhead of the virtual calls indeed probably won't matter much, unless these are very small routines that are called very often.

If you want, you can use whole program optimisation to let the compiler remove the virtual calls for you from the program:
Title: Re: Static Method implemented by a sub-class
Post by: Namthipao on February 19, 2016, 10:35:56 am
It is all very good Now I am finding this is the right time. And I will introduce some more flights.
TinyPortal © 2005-2018