www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implement Interface Using Super

reply Jonathan Levi <catanscout gmail.com> writes:
This works in LDC but not DMD?

```
class A : B, I {
     alias i = typeof(super).i;
}
class B {
     void i() {
         writeln("i");
     }
}
interface I {
     void i();
}
```

Is this a bug in DMD or in LDC?  How can I get this effect 
correctly?
Jan 26 2019
parent reply bauss <jj_1337 live.dk> writes:
On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi wrote:
 This works in LDC but not DMD?

 ```
 class A : B, I {
     alias i = typeof(super).i;
 }
 class B {
     void i() {
         writeln("i");
     }
 }
 interface I {
     void i();
 }
 ```

 Is this a bug in DMD or in LDC?  How can I get this effect 
 correctly?
There is no bug here. A does not implement i as a function which it should. What you want to do is to override i within A and then call super.i() in the function. An alias does not substitute an implementation and I think that's good because it could really cause some nasty hijacking bugs.
Jan 27 2019
next sibling parent bauss <jj_1337 live.dk> writes:
On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote:
 On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi wrote:
 This works in LDC but not DMD?

 ```
 class A : B, I {
     alias i = typeof(super).i;
 }
 class B {
     void i() {
         writeln("i");
     }
 }
 interface I {
     void i();
 }
 ```

 Is this a bug in DMD or in LDC?  How can I get this effect 
 correctly?
There is no bug here. A does not implement i as a function which it should. What you want to do is to override i within A and then call super.i() in the function. An alias does not substitute an implementation and I think that's good because it could really cause some nasty hijacking bugs.
Should probably have posted solution: ``` class A : B, I { override void i() { super.i(); } } class B { void i() { writeln("i"); } } interface I { void i(); } ```
Jan 27 2019
prev sibling parent reply Jonathan Levi <catanscout gmail.com> writes:
On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote:
 On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi wrote:
 This works in LDC *but not* DMD?
 . . .
 Is this a bug in DMD *or* in LDC?
There is no bug here.
So... LDC is the one that is bugged? I think it would have been nice to have a way of explicitly use the super method to implement an interface without having to rewrite the whole signature. I thought I remember seeing a way once, but I must have been dreaming. Thanks Bauss. Jonathan
Jan 28 2019
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/28/19 3:28 PM, Jonathan Levi wrote:
 On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote:
 On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi wrote:
 This works in LDC *but not* DMD?
 . . .
 Is this a bug in DMD *or* in LDC?
There is no bug here.
So... LDC is the one that is bugged?
Yeah, that's odd. It should be the same result, as they both have the same semantics for the front end. I'll defer to an LDC developer to answer that, but in truth, it really should be the way LDC implements it, even if that's not how the language spec is.
 I think it would have been nice to have a way of explicitly use the 
 super method to implement an interface without having to rewrite the 
 whole signature.  I thought I remember seeing a way once, but I must 
 have been dreaming.
I agree. BTW, the typeof(super) requirement is super-annoying. alias x = super.x; is clear, I don't see why we need to specify typeof(super) in this context at least. -Steev
Jan 28 2019
parent reply Meta <jared771 gmail.com> writes:
On Monday, 28 January 2019 at 22:17:56 UTC, Steven Schveighoffer 
wrote:
 On 1/28/19 3:28 PM, Jonathan Levi wrote:
 On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote:
 On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi 
 wrote:
 This works in LDC *but not* DMD?
 . . .
 Is this a bug in DMD *or* in LDC?
There is no bug here.
So... LDC is the one that is bugged?
Yeah, that's odd. It should be the same result, as they both have the same semantics for the front end. I'll defer to an LDC developer to answer that, but in truth, it really should be the way LDC implements it, even if that's not how the language spec is.
 I think it would have been nice to have a way of explicitly 
 use the super method to implement an interface without having 
 to rewrite the whole signature.  I thought I remember seeing a 
 way once, but I must have been dreaming.
I agree. BTW, the typeof(super) requirement is super-annoying. alias x = super.x; is clear, I don't see why we need to specify typeof(super) in this context at least. -Steev
It's because aliases do not support context pointers, I'm pretty sure.
Jan 28 2019
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, January 28, 2019 10:41:55 PM MST Meta via Digitalmars-d-learn 
wrote:
 On Monday, 28 January 2019 at 22:17:56 UTC, Steven Schveighoffer

 wrote:
 On 1/28/19 3:28 PM, Jonathan Levi wrote:
 On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote:
 On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi

 wrote:
 This works in LDC *but not* DMD?
 . . .
 Is this a bug in DMD *or* in LDC?
There is no bug here.
So... LDC is the one that is bugged?
Yeah, that's odd. It should be the same result, as they both have the same semantics for the front end. I'll defer to an LDC developer to answer that, but in truth, it really should be the way LDC implements it, even if that's not how the language spec is.
 I think it would have been nice to have a way of explicitly
 use the super method to implement an interface without having
 to rewrite the whole signature.  I thought I remember seeing a
 way once, but I must have been dreaming.
I agree. BTW, the typeof(super) requirement is super-annoying. alias x = super.x; is clear, I don't see why we need to specify typeof(super) in this context at least. -Steev
It's because aliases do not support context pointers, I'm pretty sure.
Yeah. It would be like trying to do something like alias x = this.x; As it stands, I believe that super is always either used as a function call to the constructor or to mean the this pointer for the base class. I don't think that it ever means the type of the base class - just like this never means the type of the current class or struct. And their usage is pretty much identical. They're both either used for calling a constructor or for accessing the pointer/reference of the object. It's just that one of them is for the current class or struct, whereas the other is for a base class of the current class. The only difference in syntax that I can think of between them at the moment is that this is also used to name constructors when they're declared, whereas super is not used in that sort of way (since any constructor that would be referenced by super would be declared with this, not super). - Jonathan M Davis
Jan 29 2019
parent Meta <jared771 gmail.com> writes:
On Wednesday, 30 January 2019 at 01:02:37 UTC, Jonathan M Davis 
wrote:
 Yeah. It would be like trying to do something like

 alias x = this.x;

 As it stands, I believe that super is always either used as a 
 function call to the constructor or to mean the this pointer 
 for the base class. I don't think that it ever means the type 
 of the base class - just like this never means the type of the 
 current class or struct. And their usage is pretty much 
 identical. They're both either used for calling a constructor 
 or for accessing the pointer/reference of the object. It's just 
 that one of them is for the current class or struct, whereas 
 the other is for a base class of the current class. The only 
 difference in syntax that I can think of between them at the 
 moment is that this is also used to name constructors when 
 they're declared, whereas super is not used in that sort of way 
 (since any constructor that would be referenced by super would 
 be declared with this, not super).

 - Jonathan M Davis
Current, you *can* use `super` to mean the type of the base class, but it's been deprecated in a recent release (IIRC): class Super { } class Sub { super test() { return new Super(); } } void main() { (new Sub()).test(); } From DPaste: Up to 2.080.1: Success and no output Since 2.081.2: Success with output: onlineapp.d(7): Deprecation: Using `super` as a type is deprecated. Use `typeof(super)` instead
Jan 29 2019