Restriction on interface function types
Steven Schveighoffer
schveiguy at yahoo.com
Wed Mar 12 06:12:21 PDT 2014
On Wed, 12 Mar 2014 09:05:05 -0400, Steve Teale
<steve.teale at britseyeview.com> wrote:
> On Wednesday, 12 March 2014 at 11:13:00 UTC, monarch_dodra wrote:
>>
>> That said, I'd expect this to work:
>>
>> //----
>> interface I
>> {
>> I myType();
>> }
>>
>> class A: I
>> {
>> //auto myType() { return cast(A) null; } //Nope
>> A myType() { return cast(A) null; } //OK
>> }
>> //----
>>
>> Yup, covariance desired, but
>
> import std.stdio;
>
> interface I
> {
> I myType();
> }
>
> class A: I
> {
> A myType() { return cast(A) null; }
> void foo() { writeln("foo"); }
> }
>
> void main()
> {
> I a = new A();
> writeln(typeof(a.myType()).stringof);
> }
>
> returns I
>
> Seems like a bug to me.
No, not a bug. What you want is actually not possible.
To demonstrate further:
void bad(I i)
{
typeof(i.myType()) x;
}
class A : I
{
A myType() { return cast(A)null;}
}
class B : I
{
B myType() {return cast(B) null;}
}
void main()
{
I[] arr = [new A, new B];
foreach(i; arr) {bad(i);}
}
How is the compiler to build it's one copy of bad? Should x be typed as A
or B? Or something not even seen in this module that could derive from I?
-Steve
More information about the Digitalmars-d
mailing list