Will Delphi support record inheritance? If yes, it should lead to pointer inheritance:
Will Delphi support record inheritance? If yes, it should lead to pointer inheritance:
type
PRecA = ^TRecA;
TRecA = record
..
end;
PRecB = ^TRecB;
TRecB = record(TRecA)
..
end;
and now PRecB is derived from PRecA and for example a variable of type PRecB can be used as a function's parameter of type PRecA:
var
PB: PRecB;
procedure Foo(P: PRecA);
begin
..
end;
Foo(PB);
type
PRecA = ^TRecA;
TRecA = record
..
end;
PRecB = ^TRecB;
TRecB = record(TRecA)
..
end;
and now PRecB is derived from PRecA and for example a variable of type PRecB can be used as a function's parameter of type PRecA:
var
PB: PRecB;
procedure Foo(P: PRecA);
begin
..
end;
Foo(PB);
Ondrej Kelle Yeah, my bad. To avoid virtual method issues, we would be using reintroduce or override and explicitly calling the inherited version. - i.e. like in the old school object type.
ReplyDelete> The difference is that there is no VMT, and the record data are contiguous
ReplyDeleteOK, so the request is not for inheritance, virtual methods and the like -- this is how I interpreted it -- but just to "extend" a record with more fields defining a new type? Type extensions is certainly in the realm of possibilities...
> a variable of type PRecB can be used as a function's parameter of type PRecA:
This was in the original request and pointers compatibility for the inheritance graph gets dangerously close to "we want records to behave like classes". This side I really don't like much, as I don't see classes having such a huge overhead over records for such a use.
Marco Cantù Here is some real world usecase for this feature: https://bitbucket.org/sglienke/spring4d/src/master/Source/Base/Collections/Spring.Collections.Trees.pas
ReplyDeleteThere is the TBinaryTreeNode and the pointer type for it and then there are TRedBlackTreeNode adding the red/black property to it and TRedBlackTreeNode and TRedBlackTreeNode adding data to that.
I am not using objects there because the overhead is just too much. Were I able to inherit records I would not have to duplicate and hardcast and making sure that they stay binary compatible.