Sign in Join

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

Mailing lists / fpc-devel / 2017-08

Multiple inheritance

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Ondrej Pokorny

Hello!

Has anybody thought about multiple inheritance in object pascal? I am now working on a client-server service that entirely uses ORM. I have several patterns that repeat all over and some of them do not match into the direct inheritance schema.

For now I solved multiple inheritance with include file templates (like rtl/objpas/sysutils/finah.inc and fina.inc). But it would be much nicer to have it built-in:

TBase = class(TObject)
private
   FBaseField: Integer;
protected
   procedure LoadFromNode(const aNode: TXMLNode); virtual;
public
   property BaseField: Integer read FBaseField write FBaseField;
end;
TAddressExtension = class extension for TBase
private
   FAddress: string;
protected
   procedure LoadFromNode(const aNode: TXMLNode); override;
public
   property Address: string read FAddress write FAddress;
end;
TTag = (black, gray, white, green, blue);
TTags = set of TTag;
TTagsExtension = class extension for TBase
private
   FTags: TTags;
protected
   procedure LoadFromNode(const aNode: TXMLNode); override;
public
   property Tags: TTags read FTags write FTags;
end;
TCustomer = class(TBase, TAddressExtension, TTagsExtension)
protected
   procedure LoadFromNode(const aNode: TXMLNode); override;
end;

TCustomer gets all fields and properties from TBase, TAddressExtension and TTagsExtension. The LoadFromNode virtual method chain is:

TCustomer -> TTagsExtension -> TAddressExtension -> TBase.

Any thoughs on it? Is it sensible? How do you solve multiple inheritance?

+ If such a concept was in FPC, the LCL could get rid of the multiple inheritance hacks in widgetset bindings :)

Ondrej

Reply by email

Sven Barth
to Ondrej Pokorny
"Ondrej Pokorny" wrote
Hello! Has anybody thought about multiple inheritance in object pascal? I am now

working on a client-server service that entirely uses ORM. I have several patterns that repeat all over and some of them do not match into the direct inheritance schema.
[snip]

Any thoughs on it? Is it sensible? How do you solve multiple inheritance?

The designers of Object Pascal decided on purpose against multiple inheritance as they probably saw the problems that C++ has with them. Even Java and C#, both clearly influenced by C++ don't support multiple inheritance.
Just think about one class in the inheritance graph reintroducing a virtual method with different parameters. What will the final class do then?

Object Pascal already has capabilities in place to deal with the shortcomings of single inheritance, namely interfaces (multiple of which a class may implement) and delegation ("implements" keyword). That might result in more code to write, but the result is cleaner and clearer.

Regards,
Sven

Reply by email

Ondrej Pokorny
to Sven Barth
Sven Barth wrote 9 lines
The designers of Object Pascal decided on purpose against multiple inheritance as they probably saw the problems that C++ has with them. Even Java and C#, both clearly influenced by C++ don't support multiple inheritance. Just think about one class in the inheritance graph reintroducing a virtual method with different parameters. What will the final class do then?

Nothing special.  The new virtual method is just a completely different method. Nothing to really bother about. Pascal supports overloading with no problems.

Actually what you get from the schema is a single inheritance chain. The extension classes would be automatically inserted:

TBase = class(TObject);
TBase_TAddressExtension = class(TBase);
TBase_TAddressExtension_TTagsExtension = class(TBase_TAddressExtension);
TCustomer = class(TBase_TAddressExtension_TTagsExtension);

Ondrej

Reply by email

Kostas Michalopoulos
to Ondrej Pokorny

Yeah, that would be helpful for when you need to use a more compositive approach and when you want an object to take on multiple roles (e.g. some sort of mediator or controller). Currently you need to create subclasses for every role but those classes are only use once with the mediator for a single instance per mediator and have wrap them, manage them (since class instances must always be pointers), etc and this is a lot of verbose boilerplate code to avoid a limitation of the language. Interfaces help a tiny bit but they are very limited since you cannot have something like base roles that implement most of the generic functionality but leave specializations to the subclasses.

Ondrej Pokorny wrote 28 lines
On 19.08.2017 9:27, Sven Barth via fpc-devel wrote: > The designers of Object Pascal decided on purpose against multiple > inheritance as they probably saw the problems that C++ has with them. Even > Java and C#, both clearly influenced by C++ don't support multiple > inheritance. > > Just think about one class in the inheritance graph reintroducing a > virtual method with different parameters. What will the final class do then? > > Nothing special. The new virtual method is just a completely different method. Nothing to really bother about. Pascal supports overloading with no problems. Actually what you get from the schema is a single inheritance chain. The extension classes would be automatically inserted: TBase = class(TObject); TBase_TAddressExtension = class(TBase); TBase_TAddressExtension_TTagsExtension = class(TBase_TAddressExtension); TCustomer = class(TBase_TAddressExtension_TTagsExtension); Ondrej _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email