[fpc-devel] interfaces / inheritance / again
Michael Van Canneyt
michael at freepascal.org
Mon Feb 2 22:59:28 CET 2026
On Mon, 2 Feb 2026, Martin Frb via fpc-devel wrote:
> On 02/02/2026 22:00, Michael Van Canneyt via fpc-devel wrote:
>>
>> Whether it is a good approach, there we probably differ in opinion :-)
>
>
> Well, the good approach doesn't work.
>
> interface can't have virtual/override apparently (it can but ignores).
>
>
> In a class, an overridden function can actually return a subclass of
> what the virtual method returned.
I don't understand this remark. Please explain ?
> And well, why not, it fulfills all the need.
>
> If that was possible in an interface too, then all would be good
>
> IFoo = interface
> function Itm: TItem; virtual;
Why would an actual implementation not be able to return TSubItem ?
>
> ISubFoo = interface(IFoo)
> function Itm: TSubItem; override;
>
> ISubFoo fulfils every bit of the contract of its base.
> But this does not work, so I need to look for something else.
>
> You read my description. Ideas?
Perhaps stupid question: why don't you use a generic IFoo<T> ?
based on your description, I don't understand what you're trying to accomplish.
I read your technical solution to a problem: all that is clear to me is that
you want to use generics and interfaces to mix lists, but I did not understand
the original problem: why do you need these lists, what is their purpose
etc.
Maybe you don't need lists at all. Maybe a simple iterator or visitor
pattern suffices, and you would not have the complexity you introduce
by using lists. Or indeed copying or using an include file and a macro is
all you can do: Generics don't solve all problems, they're overused IMO.
And in my experience interfaces introduce more problems than that they
solve, plus they don't mix well with generics (no generic methods in
interfaces).
In short: for me, it is hard to say what is best, based on what you gave as info.
Michael.
More information about the fpc-devel
mailing list