Hi Sam,
Sadly, eC does not have multiple inheritance, and does not have interfaces.
Multiple inheritance is a pain to implement, because you end up with a different offset for the class data members depending on which derived class you're dealing with. Given the way the eC compiler and runtime component is set up, this would not be impossible to implement, but it's currently not on my personal wishlist (which contains a lot of stuff compared the amount of time available to implement them
).
Interfaces are indeed a nice alternative to multiple inheritance, which Java and C# went with.
I never really programmed with them, although I did try to understand how they work and what you can do with them. I still don't have a firm grasp of how they are used and I am still unsure whether eC needs them or how to go about adding interface support in eC.
Your Car/Engine/Wheel does not do enough for me to see the problem you're trying to solve, and thus how you could solve it using the tools that eC already have available... For example, you do not have any virtual methods in there, and I'm really not sure how you mean to use the Car, Engine and Wheel classes individually. One feature that eC has is
Instance Virtual Methods.
You could use this to program some specifics of the Engine and Wheels that are particular to the new class making use of them (the car), by overriding some of the Engine or Wheel's methods in the Car class.
In eC it's also possible to tweak the 'this' type to a specific type, or for it to adapt to the enclosing class. This, combined with a 'holder' which would identify the 'Car' from within the Engine/Wheel would let you do something like this:
Code: Select all
class Engine
{
public void * holder;
virtual void any_object::spec();
void Start()
{
spec(holder);
}
}
class Wheel
{
public void * holder;
virtual void any_object::nos();
void Turn()
{
nos(holder);
}
}
class Car
{
Engine engine
{
this; // Tell the engine about the car
void spec()
{
// 'this' here is the Car
}
};
Wheel wheel
{
this;
void nos()
{
// 'this' here is the Car
}
};
}
class LawnMover
{
Engine engine
{
this;
void spec()
{
// 'this' here is the LawnMower
}
};
Wheel wheel
{
this;
void nos()
{
// 'this' here is the LawnMower
}
};
}
This might not be the most elegant solution for everything, but it handles a lot of such class connections well enough. In this particular scenario, the spec() method is more than likely internal to the Engine, that serves as a the connection with the Car: when dealing with an Engine instance, you would invoke other methods (like Start() in the example), as opposed to invoking spec() directly.
Another thing which is available in eC is the '
subclass' keyword, which is used to refer to a particular derived class of a specified class. This can be used alongside 'thisless' virtual methods, and an
any_object parameter, to be used with any unrelated class. This way you can sort of implement 'interfaces' (which cannot have data members). This supports the '.' member operator for the subclass type to behave like an instance. Here is an example to demonstrate how this can be used:
Code: Select all
class Engine
{
virtual void ::spec(any_object object);
}
class Wheel
{
virtual void ::nos(any_object object);
}
class CarWheel : Wheel
{
void spec(Car car)
{
}
}
class CarEngine : Engine
{
void nos(Car car)
{
}
}
class Car
{
subclass(Engine) engine; engine = class(CarEngine);
subclass(Wheel) wheel; wheel = class(CarWheel);
}
void SomethingThatDealsWithAnEngine(subclass(Engine) engineClass, any_object i)
{
engineClass.spec(i);
}
void Test()
{
Car car { };
SomethingThatDealsWithAnEngine(car.engine, car);
}
This gives you some sense of multiple inheritance (yet, different from the earlier example). I'm still confused as for which of these 2 scenarios (If any
) interfaces / MI are commonly used for... Here what's cumbersome is you need both a class and an instance to invoke a method on the object. Perhaps some new eC syntactic sugar could improve the usability of this technique?
Please comment whether you would see either of them useful in the interfaces/MI scenarios you had in mind!
To sum it up, eC has neither interfaces nor multiple inheritances, but it has some tools that can serve similar purposes. However, there is still room for improvement, and hopefully in the future we will handle these things more elegantly!
All the best,
Jerome