Jump to page: 1 2
Thread overview
Implementing interfaces using alias this
Jun 14, 2017
Balagopal Komarath
Jun 14, 2017
ketmar
Jun 14, 2017
Balagopal Komarath
Jun 14, 2017
ketmar
Jun 14, 2017
Balagopal Komarath
Jun 14, 2017
ketmar
Jun 14, 2017
Mike B Johnson
Jun 14, 2017
ketmar
Jun 14, 2017
Balagopal Komarath
Jun 14, 2017
basile b.
Jun 14, 2017
Balagopal Komarath
Jun 15, 2017
basile b.
Jun 15, 2017
Biotronic
Jun 15, 2017
Balagopal Komarath
Jun 15, 2017
Jesse Phillips
Jun 16, 2017
Biotronic
Jun 16, 2017
Jesse Phillips
June 14, 2017
Why doesn't this work? The Test!Duck type has a void quack() method but the compiler says it is not implemented.

import std.stdio;

interface IDuck
{
    void quack();
}

class Test(T) : IDuck
{
    T data;
    alias data this;
}

struct Duck
{
    void quack()
    {
        writeln("Quack");
    }
}


void main()
{
	Test!Duck d;
}
June 14, 2017
Balagopal Komarath wrote:

> Why doesn't this work? The Test!Duck type has a void quack() method but the compiler says it is not implemented.

'cause `alias this` is *not* a tool that can be used to emulate inheritance. no, `quack` is NOT impemented. `alias this` won't automagically paste the code.
June 14, 2017
On Wednesday, 14 June 2017 at 09:41:49 UTC, ketmar wrote:
> Balagopal Komarath wrote:
>
>> Why doesn't this work? The Test!Duck type has a void quack() method but the compiler says it is not implemented.
>
> 'cause `alias this` is *not* a tool that can be used to emulate inheritance. no, `quack` is NOT impemented. `alias this` won't automagically paste the code.

Thanks for the reply. Is there any reason for disallowing this? AFAIK, the alias this guarantees that the interface provided by Test!T is a superset of the interface provided by T. And, when T = Duck, T provides everything required by IDuck. Couldn't the compiler check while instantiating that Test!Duck provides all methods required by IDuck?
June 14, 2017
Balagopal Komarath wrote:

> On Wednesday, 14 June 2017 at 09:41:49 UTC, ketmar wrote:
>> Balagopal Komarath wrote:
>>
>>> Why doesn't this work? The Test!Duck type has a void quack() method but the compiler says it is not implemented.
>>
>> 'cause `alias this` is *not* a tool that can be used to emulate inheritance. no, `quack` is NOT impemented. `alias this` won't automagically paste the code.
>
> Thanks for the reply. Is there any reason for disallowing this? AFAIK, the alias this guarantees that the interface provided by Test!T is a superset of the interface provided by T. And, when T = Duck, T provides everything required by IDuck. Couldn't the compiler check while instantiating that Test!Duck provides all methods required by IDuck?

besides improper using of `alias this`, there is a technical reason too: `struct` is not a `class`, it doesn't have VMT, and other class goodies. as i said, `alias this` is not a macro, it doesn't paste code, it just tells the compiler to make "symbol redirection" on typechecking. interfaces *require* a full-featured class, with VMT, and some hidden pointers to support hidden interface machinery.
June 14, 2017
On Wednesday, 14 June 2017 at 09:41:49 UTC, ketmar wrote:
> Balagopal Komarath wrote:
>
>> Why doesn't this work? The Test!Duck type has a void quack() method but the compiler says it is not implemented.
>
> 'cause `alias this` is *not* a tool that can be used to emulate inheritance. no, `quack` is NOT impemented. `alias this` won't automagically paste the code.

I don't think it has to do with pasting code.

d.Quack() is well defined through the alias. Inheritance requires that a Quack() exists, and it does, through the alias.

The compiler could easily create an implementation wrapper that uses the alias this.

I believe it is simply because the logic is not implement in the compiler, not because there is some logic issue in it.
import std.stdio;

interface IDuck
{
    void quack();
}

class Test(T) : IDuck
{
    T data;
    alias data this;
    void quack() { data.quack(); }
}

struct Duck
{
    void quack()
    {
        writeln("Quack");
    }
}


void main()
{
	Test!Duck d;
	d.quack();
}

which, unfortunately causes a segmentation fault ;)(from dpaste) (even if you remove IDuck) https://dpaste.dzfl.pl/69ddb3f2b1e9

The main issue is that the compiler would have to parse the alias and see if it has members that are to be used, not hard... but this is probably not standard behavior and might lead to other problems down the road(specially with multiple alias this). Basically, which quack to call? I think it's better to require it to be explicit(as I have tried to do).



June 14, 2017
Mike B Johnson wrote:

> I don't think it has to do with pasting code.
>
> d.Quack() is well defined through the alias. Inheritance requires that a Quack() exists, and it does, through the alias.
>
> The compiler could easily create an implementation wrapper that uses the alias this.

this is called "code pasting".
June 14, 2017
On Wednesday, 14 June 2017 at 11:40:02 UTC, ketmar wrote:
> interfaces *require* a full-featured class, with VMT, and some hidden pointers to support hidden interface machinery.

I don't think that is the problem here. The type Test!Duck is a class and that type is the one implementing the interface.

June 14, 2017
On Wednesday, 14 June 2017 at 12:35:05 UTC, Mike B Johnson wrote:
> void main()
> {
> 	Test!Duck d;
> 	d.quack();
> }
>
> which, unfortunately causes a segmentation fault ;)

I think that is because you are not initializing d using new Test!Duck();
June 14, 2017
On 6/14/17 5:34 AM, Balagopal Komarath wrote:
> Why doesn't this work? The Test!Duck type has a void quack() method but
> the compiler says it is not implemented.
>
> import std.stdio;
>
> interface IDuck
> {
>     void quack();
> }
>
> class Test(T) : IDuck
> {
>     T data;
>     alias data this;
> }
>
> struct Duck
> {
>     void quack()
>     {
>         writeln("Quack");
>     }
> }
>
>
> void main()
> {
>     Test!Duck d;
> }

An alias never causes implementation to be emitted. It should just be a forward from one symbol name to another. You need an actual function that can go into the vtable that takes a 'Test!Duck' as the context pointer. This wrapper has to be written by you.

-Steve
June 14, 2017
Balagopal Komarath wrote:

> On Wednesday, 14 June 2017 at 11:40:02 UTC, ketmar wrote:
>> interfaces *require* a full-featured class, with VMT, and some hidden pointers to support hidden interface machinery.
>
> I don't think that is the problem here. The type Test!Duck is a class and that type is the one implementing the interface.

*sighs*
« First   ‹ Prev
1 2