SRL Archive - Lape

Multiple inheritance?


bonsai

2014-08-12 16:55:42

Is it correct there's no way to do multiple inheritance in lape?

Like:
program obj;

type
Tbase1 = record
base1i: integer;
end;
Tbase2 = record
base2i: integer;
end;

Tobj = record(Tbase1)(Tbase2)
obji: integer;
end;

var
o: Tobj;

begin
o.base1i := 22;
o.base2i := 44;
o.obji := 66;
writeln(o);
end.

This way is fine

program obj;

type
Tbase1 = record
base1i: integer;
end;
Tbase2 = record(Tbase1)
base2i: integer;
end;

Tobj = record(Tbase2)
obji: integer;
end;

var
o: Tobj;

begin
o.base1i := 22;
o.base2i := 44;
o.obji := 66;
writeln(o);
end.

Brandon

2014-08-12 17:39:57

Multiple inheritance and Multi-Level inheritance are two different things.. Your example shows that you are mixing them up.

Multiple:

class animal
{
};

class pet
{
};

class cat : private animal, pet
{
};


In the above, cat can access members of both animal AND pet. However, pet and animal aren't related. Pet CANNOT access members of animal and vice-versa.


Multi-level:

class animal
{
};

class pet : private animal
{
};

class cat : pet
{
};


In the above, cat can access members of both animal AND pet. However, pet CAN access animal's members.


The difference? I can have pets that aren't animals in multiple inheritance. But in multi-level inheritance, the pet has to be an animal or there is no relationship at all.

HAS-A vs. IS-A relationships.

So which did you really need? It is true that Simba doesn't support multiple inheritance but that is because it doesn't support classes either AND raises the question of why you really need multiple inheritance in SIMBA. It's also true that Simba doesn't "natively" support Linked-Lists but that doesn't mean you cannot do them.

You can simulate inheritance and multiple inheritance with a "Tagged-Union":
http://en.wikipedia.org/wiki/Tagged_union

It's a bad idea though.. Simba does support Unions btw.. afaik.

bonsai

2014-08-12 18:33:20

From: Brandon
So which did you really need? It is true that Simba doesn't support multiple inheritance but that is because it doesn't support classes either AND raises the question of why you really need multiple inheritance in SIMBA. It's also true that Simba doesn't "natively" support Linked-Lists but that doesn't mean you cannot do them.


I figured it wasn't supported, thanks for the confirmation.

Not sure I need it but if I start using more single inheritance I could see something like that coming up.

Mr. Roboto

2014-08-14 10:13:17

From: bonsai
I figured it wasn't supported, thanks for the confirmation.

Not sure I need it but if I start using more single inheritance I could see something like that coming up.


I'm not sure if you can do this in lape, but if you are ever using a language that doesn't support multiple inheritance (like C# or Java) and you need multiple inheritance then you can use the Decorator Design pattern to solve the problem.

superuser

2014-08-14 10:52:58

From: KLight
I'm not sure if you can do this in lape, but if you are ever using a language that doesn't support multiple inheritance (like C# or Java) and you need multiple inheritance then you can use the Decorator Design pattern to solve the problem.


Imho, if you ever need to use multiple inheritance, you're doing something wrong :) There is several issues with it, namely function overriding.

Brandon

2014-08-14 11:50:48

From: superuser
Imho, if you ever need to use multiple inheritance, you're doing something wrong :) There is several issues with it, namely function overriding.


I disagree. The only times you would ever have problems with it is if you did something extremely stupid and even then, it can be solved by making the class the inheritance scheme virtual or casting to specified parent and calling the overridden method. You can even bring the parent into scope and call the method on it.

There is no excuse to NOT use it in the cases where it should be used (mixins of generics, long chain cyclical dependencies). Python provides Mixins.. Java provides interfaces. Neither of which are a replacement for multiple inheritance or quite as powerful. A class can have properties from 2 parents.

A child can have 2 parents. Why force it to have 1 parent + composition IF it needs properties from both parents? The diamond inheritance problem arises from idiots and can be solved easily by the same people.

Like anything in programming, there are always features that can be misused. I always see others bashing Singletons for no reason at all. It is useful when the time comes.

Mr. Roboto

2014-08-14 11:51:25

From: superuser
Imho, if you ever need to use multiple inheritance, you're doing something wrong :) There is several issues with it, namely function overriding.


I used to think like that until I had to work on a game which copied Pokemon.

We had several types for monsters: fire, water, grass, … electric

So when we had to create a monster that was fire + water a new class had to be created because the code below would not compile in C#.

Pikachu : Fire, Water

Now imagine if you had to create a class for all the combinations. And no interfaces could be used because the attacks were all the same in those abstract base classes. So we used Decorator, it is super sexy if you ask me, and it is the go-to solution if you can't do multiple inheritance or don't want to.

superuser

2014-08-14 16:18:44

From: Brandon
I disagree. The only times you would ever have problems with it is if you did something extremely stupid and even then, it can be solved by making the class the inheritance scheme virtual or casting to specified parent and calling the overridden method. You can even bring the parent into scope and call the method on it.

There is no excuse to NOT use it in the cases where it should be used (mixins of generics, long chain cyclical dependencies). Python provides Mixins.. Java provides interfaces. Neither of which are a replacement for multiple inheritance or quite as powerful. A class can have properties from 2 parents.

A child can have 2 parents. Why force it to have 1 parent + composition IF it needs properties from both parents? The diamond inheritance problem arises from idiots and can be solved easily by the same people.

Like anything in programming, there are always features that can be misused. I always see others bashing Singletons for no reason at all. It is useful when the time comes.


Yes, perhaps, however, my 20-year career of programming has set me in this state of mind, where there is no returning back :) Call me stubborn!

I agree, singleton-paradigm can be very useful, but one has to be careful. For example, one of my co-workers forgot that they carry state around for the lifetime of the application. Dear lord, it sucked fixing the damage in production environment. Also, problem of tightly coupled code can arise. Very hard to unit-test code (read mocking singleton classes).

Mr. Roboto

2014-08-15 01:21:30

From: superuser
Yes, perhaps, however, my 20-year career of programming has set me in this state of mind, where there is no returning back :) Call me stubborn!

I agree, singleton-paradigm can be very useful, but one has to be careful. For example, one of my co-workers forgot that they carry state around for the lifetime of the application. Dear lord, it sucked fixing the damage in production environment. Also, problem of tightly coupled code can arise. Very hard to unit-test code (read mocking singleton classes).


Do you do a lot of software testing? If you don't mind me asking. The reason I asked is because I find that testers hate multiple inheritance and most design patterns, except the Facade.