www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Interface inheritance

reply Mandeep Singh Brar <mandeep brars.co.in> writes:
I just posted a bug on the bugs list about problems in compiling the following
code using dmd, but realized
that there might be some inheritance feature that i might now be aware of. Can
you please let me know if i
am missing something in the below code.

import std.stdio;
interface A {
        public void a(int l);
}
class ACl:A {
        public void a(int l) {
                writeln("Hello a");
        }
}

interface B: A {
        public void a(string l, int k);
}

class BCl: ACl, B {
        public void a(string l, int k) {
                writeln("Hello B.a", l, k);
        }
}

int main() {
        B b = new BCl();
        b.a(1);
        return 0;
}

However casting B to A like (cast(A)b).a(1); makes it work.

Mandeep
Dec 12 2010
parent reply Adam Burton <adz21c gmail.com> writes:
Mandeep Singh Brar wrote:

 I just posted a bug on the bugs list about problems in compiling the
 following code using dmd, but realized that there might be some
 inheritance feature that i might now be aware of. Can you please let me
 know if i am missing something in the below code.
 
 import std.stdio;
 interface A {
         public void a(int l);
 }
 class ACl:A {
         public void a(int l) {
                 writeln("Hello a");
         }
 }
 
 interface B: A {
         public void a(string l, int k);
 }
 
 class BCl: ACl, B {
         public void a(string l, int k) {
                 writeln("Hello B.a", l, k);
         }
 }
 
 int main() {
         B b = new BCl();
         b.a(1);
         return 0;
 }
 
 However casting B to A like (cast(A)b).a(1); makes it work.
 
 Mandeep
Try this interface B : A { alias A.a a; public void a(string l, int k); } I think this comes from hijacking. A derived type (B) provides a new overloads to a method in the base type (A). I think the idea is to avoid accidentally invoking base type overloads where they are not wanted so you have to explicitly let them through. For example if A.a was not there when you first created B, A being modified to include A.a will not affect code previously coded with B.a unless you explicitly let A.a in using alias. However once the alias is in it seems it is up to the programmer to enforce this rule if A is modified further. http://www.digitalmars.com/d/2.0/hijack.html http://www.digitalmars.com/d/archives/digitalmars/D/aliasing_base_methods_49572.html#N49577
Dec 12 2010
parent Mandeep Singh Brar <mandeep brars.co.in> writes:
But that idea would not work in APIs like JDBC for example (something
i was trying to immitate partly). PreparedStatement has method
setInt(int, int) while CallableStatement has method
setInt(string,int).

It is not possible to compile CallableStatement.setInt(int,int)
without casting or putting in aliases to a number of methods of
PreparedStatement in CallableStatement.
Dec 12 2010