digitalmars.D.learn - Implement Interface Using Super
- Jonathan Levi (16/16) Jan 26 2019 This works in LDC but not DMD?
- bauss (7/23) Jan 27 2019 There is no bug here.
- bauss (17/43) Jan 27 2019 Should probably have posted solution:
- Jonathan Levi (8/13) Jan 28 2019 So... LDC is the one that is bugged?
- Steven Schveighoffer (11/24) Jan 28 2019 Yeah, that's odd. It should be the same result, as they both have the
- Meta (4/29) Jan 28 2019 It's because aliases do not support context pointers, I'm pretty
- Jonathan M Davis (17/53) Jan 29 2019 Yeah. It would be like trying to do something like
- Meta (23/40) Jan 29 2019 Current, you *can* use `super` to mean the type of the base
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
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
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:Should probably have posted solution: ``` class A : B, I { override void i() { super.i(); } } class B { void i() { writeln("i"); } } interface I { void i(); } ```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
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: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. JonathanThis works in LDC *but not* DMD? . . . Is this a bug in DMD *or* in LDC?There is no bug here.
Jan 28 2019
On 1/28/19 3:28 PM, Jonathan Levi wrote:On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote: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.On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi wrote:So... LDC is the one that is bugged?This works in LDC *but not* DMD? . . . Is this a bug in DMD *or* in LDC?There is no bug here.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
On Monday, 28 January 2019 at 22:17:56 UTC, Steven Schveighoffer wrote:On 1/28/19 3:28 PM, Jonathan Levi wrote:It's because aliases do not support context pointers, I'm pretty sure.On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote: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.On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi wrote:So... LDC is the one that is bugged?This works in LDC *but not* DMD? . . . Is this a bug in DMD *or* in LDC?There is no bug here.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
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: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 DavisOn 1/28/19 3:28 PM, Jonathan Levi wrote:It's because aliases do not support context pointers, I'm pretty sure.On Sunday, 27 January 2019 at 09:31:46 UTC, bauss wrote: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.On Sunday, 27 January 2019 at 05:37:57 UTC, Jonathan Levi wrote:So... LDC is the one that is bugged?This works in LDC *but not* DMD? . . . Is this a bug in DMD *or* in LDC?There is no bug here.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 29 2019
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 DavisCurrent, 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