Multi inheritance or interface in eC

General help with the eC language.
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Multi inheritance or interface in eC

Post by samsam598 »

Greetings,

I was wondering how does eC implement the subjected in the language?
Given below code in C++ and C#,how does eC implement the same function in class Car?

Thanks for the help in advance.

C++:

Code: Select all

 
class Engine
{
    void spec();
}
class Wheel
{
    void nos();
}
 
class Car:public Engine,public Wheel
{
  ...  
}
 
C#:

Code: Select all

 
interface Engine
{
    void spec();
}
interface Wheel
{
   void nos();
}
public class Car:Enginie,Wheel
{
  ...
}
 
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Multi inheritance or interface in eC

Post by jerome »

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
rfc_club
Posts: 1
Joined: Wed Sep 04, 2013 11:39 am

Re: Multi inheritance or interface in eC

Post by rfc_club »

I really hope that eC can support multiple interfaces.:D
Normally it can be treated as a decoupling part when you want to change your implementation in future. Besides it defines the contract when working in team, to make sure that the change in one side do not effect to other side.
Take an example:
public interface FinancialAccount {
void deposit(int amount);
int checkout();
}
public class AtmAccount : FinancialAccount { // concrete implementation of deposit & checkout }
public class BankAccount : FinancialAccount { // concrete implementation of deposit & checkout }
public class FinalcialItems {
List<FinalcialService> list;
}
Assume that business logic of each account is differ.
eC seems a wonderful language, and it can be more better with interface. Please, I really hop that eC can support it in next release :D.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Multi inheritance or interface in eC

Post by jerome »

Thanks for the suggestion rfc_club!

Multiple interfaces support would indeed be nice, and they are planned for 0.45 right now: http://ecere.com/mantis/view.php?id=514. It may be a while seems that becomes available, but it's already on the roadmap :)

At the moment you can actually sort of manually implement interfaces, as the previous examples above describes. Here's a slightly different twist using your example:

Code: Select all

// A base class for classes implementing interfaces
public class ExtensibleClass
{
public:
   virtual Class QueryInterface(Class c);
}
 
// Definition of the FinancialAccountInterface
public class FinancialAccountInterface
{
   virtual void Instance::deposit(int amount);
   virtual int Instance::checkout(); 
}
 
public class AtmAccount : ExtensibleClass
{
   Class QueryInterface(Class c)
   {
      // Return the interfaces this class supports
      if(c == class(FinancialAccountInterface)) return class(AtmAccountFinancialAccountInterface);
      return null;
   }
 
   void deposit(int amount)
   {
 
   }
 
   int checkout()
   {
 
   }
}
 
// Present the AtmAccount methods as a FinancialAccountInterface
class AtmAccountFinancialAccountInterface : FinancialAccountInterface
{
   void deposit(int amount)
   {
      ((AtmAccount)this).deposit(amount);
   }
   int checkout()
   {
      return ((AtmAccount)this).checkout();
   }
}
 
public class BankAccount : ExtensibleClass
{
   Class QueryInterface(Class c)
   {
      // Return the interfaces this class supports
      if(c == class(FinancialAccountInterface)) return class(BankAccountFinancialAccountInterface);
      return null;
   }
 
   void deposit(int amount)
   {
 
   }
 
   int checkout()
   {
 
   }
}
 
// Present the BankAccount methods as a FinancialAccountInterface
class BankAccountFinancialAccountInterface : FinancialAccountInterface
{
   void deposit(int amount)
   {
      ((BankAccount)this).deposit(amount);      
   }
 
   int checkout()
   {
      return ((BankAccount)this).checkout();
   }
}
 
public class FinancialService : ExtensibleClass
{
public:
   void deposit(int amount)
   {
      subclass(FinancialAccountInterface) faInterface = (subclass(FinancialAccountInterface))QueryInterface(class(FinancialAccountInterface));
      if(faInterface) faInterface.deposit(this, amount);
   }
 
   int checkout()
   {
      subclass(FinancialAccountInterface) faInterface = (subclass(FinancialAccountInterface))QueryInterface(class(FinancialAccountInterface));
      return faInterface ? faInterface.checkout(this) : 0;
   }
 
   // Accept any ExtensibleClass object (They may implement the interface)
   property ExtensibleClass { }
}
 
void test()
{
   BankAccount ba { };
   AtmAccount aa { };
   List<FinancialService> services { [ aa, ba ] };
 
   for(s : services)
   {
      s.deposit(10);
      s.checkout();
   }
}
Now I realize this is quite a bit of extra code... Now let me think of what eC with interfaces support would look like :D
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Multi inheritance or interface in eC

Post by jerome »

The equivalent eC code with interfaces support could look something like:

Code: Select all

public interface FinancialAccount
{
   void deposit(int amount);
   int checkout(); 
}
 
public class AtmAccount implements FinancialAccount
{
   void deposit(int amount)
   {
 
   }
 
   int checkout()
   {
 
   }
}
 
public class BankAccount implements FinancialAccount
{
   void deposit(int amount)
   {
 
   }
 
   int checkout()
   {
 
   }
}
 
void test()
{
   BankAccount ba { };
   AtmAccount aa { };
   List<FinancialAccount> services { [ aa, ba ] };
 
   for(s : services)
   {
      s.deposit(10);
      s.checkout();
   }
}
Regards,

Jerome
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Multi inheritance or interface in eC

Post by samsam598 »

Could you please consider not adding new key word implements which is sort of VB.net verbos style.In c/c++/eC I prefer : and . pairs.

Code: Select all

 
interface Race{}
class Dog:Animal,Race{}
 
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Multi inheritance or interface in eC

Post by jerome »

Sam,

The 'implements' was to distinguish interface implementation vs class inheritance...

class MyClass : MyBaseClass implements Engine, SafetyFeatures


An interface will be defined somewhere else most of the time, so this is to make it obvious.

And since interfaces got popular with Java, I'm just following the Java trend =)

What do you think?
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Multi inheritance or interface in eC

Post by samsam598 »

Could IDE help to identify which is a class and which is an interface?

Anyway this is a taste choice by one own and I will accept `implements' as well.To my own,I prefer the less key word the better.

I will be more than happy and always loving ecere as long as it keeps C's simplicity,purity,real time direct access to hardware and OS;on the contrary,hope there will not be another Java or C++---Having every POWERFUL feature or making everything class and OOP is NOTHING.As I mentioned before,the longer time using eC,I appreciated more the language's beauty.

If there is a wish---I hope there will be more visual controls in the designer to drag and drop to the form designer,such as menu editor,tool bar designer and so on;on the language level,hope c99 could be supported to ease the interaction with exsiting third party sources and libraries which may use c99's feathers.

Best regards,
Sam
Last edited by samsam598 on Wed Oct 02, 2013 7:01 am, edited 1 time in total.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Multi inheritance or interface in eC

Post by jerome »

The IDE helping to identify which is a class/interface -- We could do that, but eC code is not always going to be looked at or edited in the Ecere IDE :) One of the design goals of the SDK is that the language, toolkit and IDE should each stand on its own (and inter-operate with others).

We're always very happy when people are appreciating our hard work and dedication :)

These goals you mention for the language are all there to stay!

The designer hasn't received much attention in a very long time, I was hoping and planning for all these features you're mentioning but there's just too much work right now and too little developers to focus on this. One of the reasons it's lower in priority is that eC is so elegant now that it's quite easy to put together these things and edit them in the code. But I'm still wishing for all that in a later version!

C99 -- I think we're slowly working our way up to have C99 compatibliity. Any more work on this though will wait until the compiler re-factoring next year which should make it much easier to support new language features.

Cheers,

Jerome
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Multi inheritance or interface in eC

Post by samsam598 »

Thanks!Knowing the fact that c++ support too much stuffs finally make itself an extremely complicated language.Trust eC won't be that.But have to say,only C++ in the world can create an object in stack ,not only on the heap.
Post Reply