www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multiple inheritance simulation (interfaces + templates)

Hello Walter and everyone.

Firstly, sorry for my bad English as it is not my native one.
Secondly, do not criticize me severely because I'm a newbie in D.

Recently, I've decided to port «JUCE» library
(http://www.rawmaterialsoftware.com/juce/) to D. The library is written in C++
using the multiple inheritance feature reasonably. I've thought how to
facilitate porting «JUCE» source code from C++ to D for myself. I think some
of D gurus already have some approaches to simulate, mimic or avoid C++'s
multiple inheritance in D. If so, please, share your technique here for me
because sometimes it's important enough to have this feature. Anyway, I think
this can be easily achieved by separating classes into the interface part and
the template interface implementation part. We encounter several problems when
doing thus:

Firstly, when declaring a constructor and destructor inside a tamplate (this
and ~this), only the destructor joins a class' destructor (because it's
virtual, I think). Ok, it's not really a problem but it would be a very
convenient way to point to the compiler that the template constructor with the
same parameters must be joined too with that of the class' one in which the
mixin occures.

//like this example
template A(T) //T is not used. Can we omit it? No! :( Y?
{
	this() {writefln(“mixin this”);} //if should be joined then place the “join”
keyword before “this”
	~this(){writefln(“mixin ~this”);}
}

class B
{
	mixin A!(void) mx_A; //Is it good to write like this: mixin A mx_A;? No! :( Y?

	this(){writefln(“class this”);}
	~this(){writefln(“class ~this”);}
}

void main()
{
	auto b = new B;
}

//the output will be:
class this
class ~this
mixin ~this

//but wish output to be the following one (maybe using the keyword «join»
before «this» of the template to achieve the result):
mixin this
class this
class ~this
mixin ~this


Secondly, There is no way to distinguish interface's functions when
implementing them in a class if there are more then one inherited interfaces
that declare functions with the same names. I wish to have the ability to
implement each interface's function separately along with the general one
(like C# does that). Those separate functions are supposed to be called only
when I use an interface to a class explicitly.  And the general ones are
supposed to be called only when I use an instance of a class. If some of the
interface function implementation is missing then the general function of the
class is called (as implemented now). Here like it must look like:

interface A
{
	int get();
	void set(int);
}

interface B
{
	int get();
	void set(int);
}

class C : A, B
{
	int A.get(){writefln(“called from interface A”);} //optional
	int B.get(){writefln(“called from interface B”);} //optional
	int get(){writefln(“called from class C”);} //called in any ambiguous state
							//or right from the instance of the class C

	void A.set(int a){writefln(“called from interface A”);} //optional
	void B.set(int a){writefln(“called from interface B”);} //optional
	void set(int a){writefln(“called from class C”);} //called in any ambiguous
state
							//or right from the instance of the class C
}

So, we must be able to access each function from the instance like:
auto c = new C;
int z = c.A.get(); or int z = (cast(A)c).get(); or {A a = c; int z = a.get();
// calls A.get()}


Now we can separate multiple inheritance into two parts: interface that
describes the functionality and template that implements that one. So, I think
that's enough for the simple multiple inheritance simulation.

The problem is we cannot implement this approach in D right now.

BTW, why doesn't the “final” keyword work as it's supposed to? A bug?
Nov 28 2006