Jump to page: 1 2 3
Thread overview
Interface Return Types
Aug 16, 2004
teqDruid
Aug 16, 2004
Derek Parnell
Aug 16, 2004
teqDruid
Aug 16, 2004
Derek
Aug 17, 2004
Deja Augustine
Aug 17, 2004
Deja Augustine
Aug 16, 2004
Walter
Aug 17, 2004
teqDruid
Aug 17, 2004
Walter
Aug 17, 2004
teqDruid
Aug 17, 2004
Walter
Aug 17, 2004
teqDruid
Aug 18, 2004
Walter
Aug 18, 2004
teqDruid
Aug 19, 2004
Walter
Aug 17, 2004
Russ Lewis
Aug 17, 2004
teqDruid
Aug 17, 2004
Russ Lewis
Aug 20, 2004
Matthias Becker
Aug 20, 2004
Agent Smith
Aug 21, 2004
Walter
August 16, 2004
Why isn't this possible? It'd be nice (actually, nearly necessary for
what I'm doing)

interface I
{
	I a();
}

class B : I
{
	B a()
	{
		return new B();
	}

	this()
	{}
}

Also, the error message:
class B 1interface function I.a is not implemented
Would be more legible if it was "interface" not "linterface", but that's
(obviously) very minor.

John
August 16, 2004
On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:

> Why isn't this possible? It'd be nice (actually, nearly necessary for
> what I'm doing)
> 
> interface I
> {
> 	I a();
> }
> 
> class B : I
> {
> 	B a()
> 	{
> 		return new B();
> 	}
> 
> 	this()
> 	{}
> }
> 
> Also, the error message:
> class B 1interface function I.a is not implemented
> Would be more legible if it was "interface" not "linterface", but that's
> (obviously) very minor.
> 
> John

Will it suit your purposes if you change it to ...

# 	I a()
# 	{
# 		return new B();
#	}


I guess what you trying to do with function 'a' is to return only those members implemented in class 'B' that derive from interface 'I'.

Here is my test using an expanded class to highlight multiple interfaces...

// -------------- <code>

import std.stdio;
interface I
{
    I a();
}
interface J
{
    void c();
}

class B : I, J
{
    I a()
    {
        return new B();
    }

    void c(){ writef("C\n"); }
}

void main()
{
    I i;
    J j;
    B b;

    b = new B;
    b.c();
    i = b.a();
    // j = b.a(); //  cannot implicitly convert I to J
    // i.c(); //  no property 'c' for type 'I'
    // j.c(); // Access Violation
}
// -------------- </code>

-- 
Derek
Melbourne, Australia
16/Aug/04 6:15:40 PM
August 16, 2004
On Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:

> On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:
> 
>> Why isn't this possible? It'd be nice (actually, nearly necessary for
>> what I'm doing)
>> 
>> interface I
>> {
>> 	I a();
>> }
>> 
>> class B : I
>> {
>> 	B a()
>> 	{
>> 		return new B();
>> 	}
>> 
>> 	this()
>> 	{}
>> }
>> 
>> Also, the error message:
>> class B 1interface function I.a is not implemented
>> Would be more legible if it was "interface" not "linterface", but that's
>> (obviously) very minor.
>> 
>> John
> 
> Will it suit your purposes if you change it to ...
> 
> # 	I a()
> # 	{
> # 		return new B();
> #	}
> 
> 
No.  It will not.  I've got a class that not only implements an interface, but adds functionality.  I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface.  One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't.

I don't see a reason why you shouldn't be able to do this...  Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do)

John
August 16, 2004
Because B is not "covariant" with I, so an instance of B cannot masquerade as an instance of I without a cast.

"teqDruid" <me@teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903@teqdruid.com...
> Why isn't this possible? It'd be nice (actually, nearly necessary for
> what I'm doing)
>
> interface I
> {
> I a();
> }
>
> class B : I
> {
> B a()
> {
> return new B();
> }
>
> this()
> {}
> }
>
> Also, the error message:
> class B 1interface function I.a is not implemented
> Would be more legible if it was "interface" not "linterface", but that's
> (obviously) very minor.
>
> John


August 16, 2004
On Mon, 16 Aug 2004 14:25:13 -0400, teqDruid wrote:

> On Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:
> 
>> On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:
>> 
>>> Why isn't this possible? It'd be nice (actually, nearly necessary for
>>> what I'm doing)
>>> 
>>> interface I
>>> {
>>> 	I a();
>>> }
>>> 
>>> class B : I
>>> {
>>> 	B a()
>>> 	{
>>> 		return new B();
>>> 	}
>>> 
>>> 	this()
>>> 	{}
>>> }
>>> 
>>> Also, the error message:
>>> class B 1interface function I.a is not implemented
>>> Would be more legible if it was "interface" not "linterface", but that's
>>> (obviously) very minor.
>>> 
>>> John
>> 
>> Will it suit your purposes if you change it to ...
>> 
>> # 	I a()
>> # 	{
>> # 		return new B();
>> #	}
>> 
>> 
> No.  It will not.  I've got a class that not only implements an interface, but adds functionality.  I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface.  One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't.

Ah, I understand. You want to describe an interface such that it has a member that returns an instance of any class that implements that interface.

> I don't see a reason why you shouldn't be able to do this...  Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do)

Yep that seems like a reasonable idea. I can't think of anyway that D can do this yet. Obviously returning 'Object' is too broad.

This code works, but maybe its too untidy for you...

----------------
import std.stdio;
interface I
{
    I a();
}

class B : I
{
    I a(){return new B();}
    this() {writef("B\n");}
    void C() { writef("C\n"); }
}

void main()
{
    B b;
    B c;

    b = new B;
    c = cast(B)b.a();  // Explicit cast!
    c.C();
}
-------------

-- 
Derek
Melbourne, Australia
August 17, 2004
Please forgive the ignorance, but I don't understand.  B implements I, so an instance of B can be used as the I type.

If B's method a()'s signature were modified to return type I, then I would
have to do the following:

B b = new B();
B myB = cast(B)b.a();

Whereas if the example worked as given the following would also work:
I i = new B();
I myI = i.a();
since B (obviously) implements I, so can be implicitly cast to it.
Unless there is some sort of compiler limitation preventing such.

Could someone explain using smaller words?

John

On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:

> Because B is not "covariant" with I, so an instance of B cannot masquerade as an instance of I without a cast.
> 
> "teqDruid" <me@teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903@teqdruid.com...
>> Why isn't this possible? It'd be nice (actually, nearly necessary for
>> what I'm doing)
>>
>> interface I
>> {
>> I a();
>> }
>>
>> class B : I
>> {
>> B a()
>> {
>> return new B();
>> }
>>
>> this()
>> {}
>> }
>>
>> Also, the error message:
>> class B 1interface function I.a is not implemented
>> Would be more legible if it was "interface" not "linterface", but that's
>> (obviously) very minor.
>>
>> John

August 17, 2004
B can be *converted* to the I type, but cannot be used *directly* as an I type. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits. But a float cannot be used as a short, because the representations are different. But a float can be converted to a short.

"teqDruid" <me@teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076@teqdruid.com...
> Please forgive the ignorance, but I don't understand.  B implements I, so an instance of B can be used as the I type.
>
> If B's method a()'s signature were modified to return type I, then I would
> have to do the following:
>
> B b = new B();
> B myB = cast(B)b.a();
>
> Whereas if the example worked as given the following would also work:
> I i = new B();
> I myI = i.a();
> since B (obviously) implements I, so can be implicitly cast to it.
> Unless there is some sort of compiler limitation preventing such.
>
> Could someone explain using smaller words?
>
> John
>
> On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:
>
> > Because B is not "covariant" with I, so an instance of B cannot
masquerade
> > as an instance of I without a cast.
> >
> > "teqDruid" <me@teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903@teqdruid.com...
> >> Why isn't this possible? It'd be nice (actually, nearly necessary for
> >> what I'm doing)
> >>
> >> interface I
> >> {
> >> I a();
> >> }
> >>
> >> class B : I
> >> {
> >> B a()
> >> {
> >> return new B();
> >> }
> >>
> >> this()
> >> {}
> >> }
> >>
> >> Also, the error message:
> >> class B 1interface function I.a is not implemented
> >> Would be more legible if it was "interface" not "linterface", but
that's
> >> (obviously) very minor.
> >>
> >> John
>



August 17, 2004
In article <pan.2004.08.16.18.25.13.608059@teqdruid.com>, teqDruid says...
>
>On Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:
>
>> On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:
>> 
>>> Why isn't this possible? It'd be nice (actually, nearly necessary for
>>> what I'm doing)
>>> 
>>> interface I
>>> {
>>> 	I a();
>>> }
>>> 
>>> class B : I
>>> {
>>> 	B a()
>>> 	{
>>> 		return new B();
>>> 	}
>>> 
>>> 	this()
>>> 	{}
>>> }
>>> 
>>> Also, the error message:
>>> class B 1interface function I.a is not implemented
>>> Would be more legible if it was "interface" not "linterface", but that's
>>> (obviously) very minor.
>>> 
>>> John
>> 
>> Will it suit your purposes if you change it to ...
>> 
>> # 	I a()
>> # 	{
>> # 		return new B();
>> #	}
>> 
>> 
>No.  It will not.  I've got a class that not only implements an interface, but adds functionality.  I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface.  One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't.
>
>I don't see a reason why you shouldn't be able to do this...  Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do)
>
>John


This may seem a daft question, but have you tried casting?

# interface I
# {
#  I a();
# }
#
# class B
# {
#  I a() { return new B(); }
#  this() {}
# }
# ...
#   B b = new B();
#   B b2 = cast(B)(b.a());

-Deja


August 17, 2004
In article <1iqrvs0lqsir0.evfkehx7osgz$.dlg@40tude.net>, Derek says...
>
>On Mon, 16 Aug 2004 14:25:13 -0400, teqDruid wrote:
>
>> On Mon, 16 Aug 2004 18:25:54 +1000, Derek Parnell wrote:
>> 
>>> On Mon, 16 Aug 2004 03:45:15 -0400, teqDruid wrote:
>>> 
>>>> Why isn't this possible? It'd be nice (actually, nearly necessary for
>>>> what I'm doing)
>>>> 
>>>> interface I
>>>> {
>>>> 	I a();
>>>> }
>>>> 
>>>> class B : I
>>>> {
>>>> 	B a()
>>>> 	{
>>>> 		return new B();
>>>> 	}
>>>> 
>>>> 	this()
>>>> 	{}
>>>> }
>>>> 
>>>> Also, the error message:
>>>> class B 1interface function I.a is not implemented
>>>> Would be more legible if it was "interface" not "linterface", but that's
>>>> (obviously) very minor.
>>>> 
>>>> John
>>> 
>>> Will it suit your purposes if you change it to ...
>>> 
>>> # 	I a()
>>> # 	{
>>> # 		return new B();
>>> #	}
>>> 
>>> 
>> No.  It will not.  I've got a class that not only implements an interface, but adds functionality.  I want whoever is using the library to use the class' extra functionality if they know it's that class, or just use the generic interface.  One problem is that a lot of my code in that class calls methods that the interface provides, and methods that it doesn't.
>
>Ah, I understand. You want to describe an interface such that it has a member that returns an instance of any class that implements that interface.
>
>> I don't see a reason why you shouldn't be able to do this...  Perhaps I'll re-post this under bugs, since it'll basically stop me dead in my tracks in a week or so (since I'll run out of other things to do)
>
>Yep that seems like a reasonable idea. I can't think of anyway that D can do this yet. Obviously returning 'Object' is too broad.
>
>This code works, but maybe its too untidy for you...
>
>----------------
>import std.stdio;
>interface I
>{
>    I a();
>}
>
>class B : I
>{
>    I a(){return new B();}
>    this() {writef("B\n");}
>    void C() { writef("C\n"); }
>}
>
>void main()
>{
>    B b;
>    B c;
> 
>    b = new B;
>    c = cast(B)b.a();  // Explicit cast!
>    c.C();
>}
>-------------
>
>-- 
>Derek
>Melbourne, Australia


Eheh... helps to refresh your browser to see that there are new posts to a topic before replying :)

-Deja


August 17, 2004
I believe I understand now. So in order for the example to work, there would have to be a mechanism to convert B to I during an implicit cast, yes?  If so, why is that mechanism not there?

Thanks for the explanation
John

On Tue, 17 Aug 2004 00:10:56 -0700, Walter wrote:

> B can be *converted* to the I type, but cannot be used *directly* as an I type. The layouts of the vtbl[]s are different. Think of it like this: a long can be used as if it was a short, by just ignoring the upper bits. But a float cannot be used as a short, because the representations are different. But a float can be converted to a short.
> 
> "teqDruid" <me@teqdruid.com> wrote in message news:pan.2004.08.17.06.57.18.155076@teqdruid.com...
>> Please forgive the ignorance, but I don't understand.  B implements I, so an instance of B can be used as the I type.
>>
>> If B's method a()'s signature were modified to return type I, then I would
>> have to do the following:
>>
>> B b = new B();
>> B myB = cast(B)b.a();
>>
>> Whereas if the example worked as given the following would also work:
>> I i = new B();
>> I myI = i.a();
>> since B (obviously) implements I, so can be implicitly cast to it.
>> Unless there is some sort of compiler limitation preventing such.
>>
>> Could someone explain using smaller words?
>>
>> John
>>
>> On Mon, 16 Aug 2004 12:20:38 -0700, Walter wrote:
>>
>> > Because B is not "covariant" with I, so an instance of B cannot
> masquerade
>> > as an instance of I without a cast.
>> >
>> > "teqDruid" <me@teqdruid.com> wrote in message news:pan.2004.08.16.07.45.15.79903@teqdruid.com...
>> >> Why isn't this possible? It'd be nice (actually, nearly necessary for
>> >> what I'm doing)
>> >>
>> >> interface I
>> >> {
>> >> I a();
>> >> }
>> >>
>> >> class B : I
>> >> {
>> >> B a()
>> >> {
>> >> return new B();
>> >> }
>> >>
>> >> this()
>> >> {}
>> >> }
>> >>
>> >> Also, the error message:
>> >> class B 1interface function I.a is not implemented
>> >> Would be more legible if it was "interface" not "linterface", but
> that's
>> >> (obviously) very minor.
>> >>
>> >> John
>>

« First   ‹ Prev
1 2 3