www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - interface is interface

reply "Pavel" <phondogo gmail.com> writes:
Hello!

import std.stdio;

interface I {
	
}

interface B : I {
	void test();
}

interface C : I {
	void test1();
}

class A : B, C {
   override void test() {}
   override void test1() {}
}

void main() {
	A a = new A();
	I b = cast(B)a;
	I c = cast(C)a;

writeln(cast(void*)a, "  ", cast(void*)b, "  ", cast(void*)c); // 
1EE1FE0  1EE1FE8  1EE1FEC

assert (a is b); // OK
assert (a is c); // FAIL WHY????
	
}
Jun 17 2014
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 17 Jun 2014 15:02:33 -0400, Pavel <phondogo gmail.com> wrote:

 Hello!

 import std.stdio;

 interface I {
 	
 }

 interface B : I {
 	void test();
 }

 interface C : I {
 	void test1();
 }

 class A : B, C {
    override void test() {}
    override void test1() {}
 }

 void main() {
 	A a = new A();
 	I b = cast(B)a;
 	I c = cast(C)a;

 writeln(cast(void*)a, "  ", cast(void*)b, "  ", cast(void*)c); //  
 1EE1FE0  1EE1FE8  1EE1FEC
b and c should be identical IMO. This means there are 2 entries for I inside the object, which is a waste of space.
 assert (a is b); // OK
This should also fail. It seems like a bug to me. 'is' should compare pointers, not anything else. -Steve
Jun 17 2014
prev sibling parent "rumbu" <rumbu rumbu.ro> writes:
On Tuesday, 17 June 2014 at 19:02:34 UTC, Pavel wrote:

 class A : B, C {
   override void test() {}
   override void test1() {}
 }
If you swap the interfaces (class A: C, B), "a is b" will return false, but "a is c" will return true. Clearly, this is a bug.
 void main() {
 	A a = new A();
 	I b = cast(B)a;
 	I c = cast(C)a;
If you remove the casts, will work as expected: I b = a; I c = a; or even: B b = a; C c = a;
Jun 17 2014