interfaces :-(

michal.minich at gmail.com michal.minich at gmail.com
Wed Jun 21 02:30:10 PDT 2006


You have to write:
(cast(A)b).compareTo(a);

In case you use only b.compareTo(a); D issues an error, because the method
comparetTo in class B hides the implementation of this method in class a, so the
parameters of type B cannot be casted to A.

btw. (OT?) in C# is very nice possibility to imlement more interfaces in one
class with the same method signature. I don't know if this is possible in
Java...?

interface IA { void foo() }
interface IB { void foo() }
interface IC { void foo() }

class D : IA, IB, IC
{
void IA.foo () {} // explicitly implements foo() in interface IA
void foo () {} // implicitly implements foo() in interface IB
void IC.foo () {} // explicitly implements foo() in interface IC
}

void main ()
{
D d = new D();
d.foo() // calls foo implementing IB interface
((IA)d).foo () // calls foo implementing IA interface
((IB)d).foo () // calls foo implementing IA interface
}

I think the reason for this in C# is to solve problems where class have to
implement two absolutely different interface, but it happens that they have the
same method signature. It would be nice to have this possiblity in D.





In article <e79q4g$c4d$1 at digitaldaemon.com>, Sean Fritz says...
>
>In article <e798bq$2gmi$1 at digitaldaemon.com>, michal.minich at gmail.com says...
>>
>>This is now possible in D, here's example, hope it's helpful
>>
>>
>>interface Comparable (T)
>>{
>>void compareTo(T o);
>>}
>>
>>class A : Comparable!(A)
>>{
>>char[] textA;
>>
>>void compareTo(A o)
>>{
>>printf ("A.compareTo: " ~ o.textA ~\n);
>>printf ( \n );
>>}	
>>}
>>
>>class B : A, Comparable!(B)
>>{
>>char[] textB;
>>
>>void compareTo(B o)
>>{
>>printf ("B.compareTo: " ~ o.textA ~\n);
>>printf ("B.compareTo: " ~ o.textB ~\n);
>>printf ( \n );
>>}
>>}
>>
>>
>>void main ()
>>{
>>A a = new A();
>>B b = new B();
>>
>>a.textA = "TextA in instance of A";
>>
>>b.textA = "TextA in instance of B";
>>b.textB = "TextB in instance of B";
>>
>>a.compareTo(a);
>>a.compareTo(b);
>>b.compareTo(b);
>>}
>
>Does B also have a method/function (which do I use with D?) compareTo(A o)?
>
>Sean
>
>





More information about the Digitalmars-d-dtl mailing list