Re: Implementing interfaces using alias this

2017-06-16 Thread Jesse Phillips via Digitalmars-d-learn
On Friday, 16 June 2017 at 08:34:21 UTC, Biotronic wrote: On Thursday, 15 June 2017 at 18:49:58 UTC, Jesse Phillips wrote: wrap!IDuck Ah, so it does exist in Phobos. I thought it should be there, but didn't find it. Thanks! -- Biotronic Yeah, when Andrei introduced the wrap function I

Re: Implementing interfaces using alias this

2017-06-16 Thread Biotronic via Digitalmars-d-learn
On Thursday, 15 June 2017 at 18:49:58 UTC, Jesse Phillips wrote: wrap!IDuck Ah, so it does exist in Phobos. I thought it should be there, but didn't find it. Thanks! -- Biotronic

Re: Implementing interfaces using alias this

2017-06-15 Thread Jesse Phillips via Digitalmars-d-learn
On Wednesday, 14 June 2017 at 09:34:27 UTC, 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. You question was answered, but you can do this: -- interface IDuck { void

Re: Implementing interfaces using alias this

2017-06-15 Thread Balagopal Komarath via Digitalmars-d-learn
On Thursday, 15 June 2017 at 07:12:56 UTC, Biotronic wrote: Here however, is a solution that works for simple examples. This is awesome. Very generic. Thanks.

Re: Implementing interfaces using alias this

2017-06-15 Thread Biotronic via Digitalmars-d-learn
On Wednesday, 14 June 2017 at 09:34:27 UTC, Balagopal Komarath wrote: void main() { Test!Duck d; } As has been pointed out at length by others here, it's simply not how alias this is intended to work. I do see some arguments in favor of working that way, but I'm not sure what's the

Re: Implementing interfaces using alias this

2017-06-14 Thread basile b. via Digitalmars-d-learn
On Wednesday, 14 June 2017 at 22:29:08 UTC, Balagopal Komarath wrote: On Wednesday, 14 June 2017 at 21:04:55 UTC, basile b. wrote: The way to do that in D is with mixins: That is an interesting solution. However, my original goal was to figure out whether one can make struct types behave

Re: Implementing interfaces using alias this

2017-06-14 Thread Balagopal Komarath via Digitalmars-d-learn
On Wednesday, 14 June 2017 at 21:04:55 UTC, basile b. wrote: The way to do that in D is with mixins: That is an interesting solution. However, my original goal was to figure out whether one can make struct types behave polymorphically (Which is not mentioned in my original question). I

Re: Implementing interfaces using alias this

2017-06-14 Thread basile b. via Digitalmars-d-learn
On Wednesday, 14 June 2017 at 09:34:27 UTC, 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

Re: Implementing interfaces using alias this

2017-06-14 Thread ketmar via Digitalmars-d-learn
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

Re: Implementing interfaces using alias this

2017-06-14 Thread Steven Schveighoffer via Digitalmars-d-learn
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 {

Re: Implementing interfaces using alias this

2017-06-14 Thread Balagopal Komarath via Digitalmars-d-learn
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();

Re: Implementing interfaces using alias this

2017-06-14 Thread Balagopal Komarath via Digitalmars-d-learn
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

Re: Implementing interfaces using alias this

2017-06-14 Thread ketmar via Digitalmars-d-learn
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

Re: Implementing interfaces using alias this

2017-06-14 Thread Mike B Johnson via Digitalmars-d-learn
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

Re: Implementing interfaces using alias this

2017-06-14 Thread ketmar via Digitalmars-d-learn
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

Re: Implementing interfaces using alias this

2017-06-14 Thread Balagopal Komarath via Digitalmars-d-learn
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

Re: Implementing interfaces using alias this

2017-06-14 Thread ketmar via Digitalmars-d-learn
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

Implementing interfaces using alias this

2017-06-14 Thread Balagopal Komarath via Digitalmars-d-learn
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");