Thread overview | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 15, 2011 Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mandeep Singh Brar | > 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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | But it is for only storage purposes. I can cast it back to the Interface later when required. Thanks Mandeep |
January 15, 2011 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mandeep Singh Brar | 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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | 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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon Attachments:
| 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 Re: Assigning Interface to Object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jérôme M. Berger | 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. |
Copyright © 1999-2021 by the D Language Foundation