Jump to page: 1 2 3
Thread overview
Assigning Interface to Object
Jan 15, 2011
Mandeep Singh Brar
Jan 15, 2011
Trass3r
Jan 15, 2011
Mandeep Singh Brar
Jan 15, 2011
Simen kjaeraas
Jan 15, 2011
Simen kjaeraas
Jan 16, 2011
Stewart Gordon
Jan 16, 2011
Jérôme M. Berger
Jan 16, 2011
Stewart Gordon
Jan 16, 2011
Simen kjaeraas
Jan 17, 2011
Simen kjaeraas
Jan 20, 2011
Mandeep Singh Brar
Jan 20, 2011
Simen kjaeraas
Jan 20, 2011
Trass3r
Jan 21, 2011
Andrej Mitrovic
Jan 21, 2011
Trass3r
Jan 23, 2011
Daniel Murphy
Jan 22, 2011
Mandeep Singh Brar
Jan 23, 2011
Daniel Murphy
Jan 20, 2011
Mandeep Singh Brar
January 15, 2011
Hi,

I am not able to assign an interface to object. The following code does not compile.

module testObj;

public interface testInterface {
	void someMethod();
}
public class testObj
{
	Object someCaller;
	this(Object caller) {
		someCaller = caller;
	}
	this(testInterface tI, bool xyz) {
		someCaller = tI;
	}
}

Shouldn't this work?

Thanks & Regards
Mandeep
January 15, 2011
> module testObj;
>
> public interface testInterface {
> 	void someMethod();
> }
> public class testObj
> {
> 	Object someCaller;
> 	this(Object caller) {
> 		someCaller = caller;
> 	}
> 	this(testInterface tI, bool xyz) {
> 		someCaller = tI;
> 	}
> }
>
> Shouldn't this work?

Doesn't really make sense.
If you cast it to Object you "loose" the interface methods.
January 15, 2011
But it is for only storage purposes. I can cast it back to the Interface later when required.

Thanks
Mandeep
January 15, 2011
Mandeep Singh Brar <mandeep@brars.co.in> wrote:

> Hi,
>
> I am not able to assign an interface to object. The following code
> does not compile.
>
> module testObj;
>
> public interface testInterface {
> 	void someMethod();
> }
> public class testObj
> {
> 	Object someCaller;
> 	this(Object caller) {
> 		someCaller = caller;
> 	}
> 	this(testInterface tI, bool xyz) {
> 		someCaller = tI;
> 	}
> }
>
> Shouldn't this work?

Nope. D allows interfaces to be special in certain cases (notably COM,
though other may be added in the future), and this precludes making
interfaces implicitly castable to Object.

-- 
Simen
January 15, 2011
Trass3r <un@known.com> wrote:

>> module testObj;
>>
>> public interface testInterface {
>> 	void someMethod();
>> }
>> public class testObj
>> {
>> 	Object someCaller;
>> 	this(Object caller) {
>> 		someCaller = caller;
>> 	}
>> 	this(testInterface tI, bool xyz) {
>> 		someCaller = tI;
>> 	}
>> }
>>
>> Shouldn't this work?
>
> Doesn't really make sense.
> If you cast it to Object you "loose" the interface methods.

Same way you lose methods of a subclass when you cast it to
a base class. That's allowed, though.

-- 
Simen
January 15, 2011
On Sat, 15 Jan 2011 11:34:05 -0500, Simen kjaeraas <simen.kjaras@gmail.com> wrote:

> Mandeep Singh Brar <mandeep@brars.co.in> wrote:
>
>> Hi,
>>
>> I am not able to assign an interface to object. The following code
>> does not compile.
>>
>> module testObj;
>>
>> public interface testInterface {
>> 	void someMethod();
>> }
>> public class testObj
>> {
>> 	Object someCaller;
>> 	this(Object caller) {
>> 		someCaller = caller;
>> 	}
>> 	this(testInterface tI, bool xyz) {
>> 		someCaller = tI;
>> 	}
>> }
>>
>> Shouldn't this work?
>
> Nope. D allows interfaces to be special in certain cases (notably COM,
> though other may be added in the future), and this precludes making
> interfaces implicitly castable to Object.

Which unnecessarily complicates things.  For example, you can't compare two interfaces (try it!).

Sorry, had to get my dig in there :P

-Steve
January 16, 2011
On 15/01/2011 17:44, Steven Schveighoffer wrote:
<snip>
> Which unnecessarily complicates things. For example, you can't compare
> two interfaces (try it!).

?
January 16, 2011
On 01/15/11 10:34, Simen kjaeraas wrote:
> Mandeep Singh Brar <mandeep@brars.co.in> wrote:
> 
>> Hi,
>>
>> I am not able to assign an interface to object. The following code does not compile.
>>
>> module testObj;
>>
>> public interface testInterface {
>>     void someMethod();
>> }
>> public class testObj
>> {
>>     Object someCaller;
>>     this(Object caller) {
>>         someCaller = caller;
>>     }
>>     this(testInterface tI, bool xyz) {
>>         someCaller = tI;
>>     }
>> }
>>
>> Shouldn't this work?
> 
> Nope. D allows interfaces to be special in certain cases (notably COM, though other may be added in the future), and this precludes making interfaces implicitly castable to Object.
> 

I believe interfaces marked 'extern(C++)' (for using classes defined by
C++) are also somewhat special.

Still, one would think that the special cases (IUnknown and extern(C++)) would be enforced without affecting "typical" interfaces.  Or is there something about how D's vtbl and other affairs are laid out that I don't know about -- such that casting from an interface to Object would lose something?

-- Chris N-S
January 16, 2011
Stewart Gordon wrote:
> On 15/01/2011 17:44, Steven Schveighoffer wrote:
> <snip>
>> Which unnecessarily complicates things. For example, you can't compare
>> two interfaces (try it!).
> 
> ?
interface I {}

...

I a = ...;
I b = ...;

if (a == b) // <-- ERROR

-- 
mailto:jeberger@free.fr
http://jeberger.free.fr
Jabber: jeberger@jabber.fr



January 16, 2011
On 16/01/2011 08:23, "Jérôme M. Berger" wrote:
> Stewart Gordon wrote:
>> On 15/01/2011 17:44, Steven Schveighoffer wrote:
>> <snip>
>>> Which unnecessarily complicates things. For example, you can't compare
>>> two interfaces (try it!).
>>
>> ?
> interface I {}
>
> ...
>
> I a = ...;
> I b = ...;
>
> if (a == b) //<-- ERROR


1.065: compiles without error, though it seems to be equivalent to is
2.051: it's really weird
----------
C:\Users\Stewart\Documents\Programming\D\Tests>dmd interface_equals.d
interface_equals.d(7): Error: function object.opEquals (Object lhs, Object rhs)
is not callable using argument types (I,I)
interface_equals.d(7): Error: cannot implicitly convert expression (a) of type i
nterface_equals.I to object.Object
interface_equals.d(7): Error: cannot implicitly convert expression (b) of type i
nterface_equals.I to object.Object
----------

Of course, if the interface declares an opEquals, it's a whole different story....

Stewart.
« First   ‹ Prev
1 2 3