Assigning Interface to Object

Christopher Nicholson-Sauls ibisbasenji at gmail.com
Sat Jan 15 23:21:54 PST 2011


On 01/15/11 10:34, Simen kjaeraas wrote:
> Mandeep Singh Brar <mandeep at 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


More information about the Digitalmars-d-learn mailing list