www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - override and interface methods

reply FeepingCreature <feepingcreature gmail.com> writes:
When overriding a class method, you are required to specify 
`override`, indicating you are redefining existing behavior.

When implementing an interface method, override *can* be used but 
doesn't *have to* be used. This seems quite ambiguous and 
confusing. Am I overriding a method here? If yes, I should be 
required to use it. If not, I should not be *allowed* to use it.

Thoughts?
Oct 29 2018
next sibling parent reply Andrea Fontana <nospam example.com> writes:
On Monday, 29 October 2018 at 13:14:28 UTC, FeepingCreature wrote:
 When overriding a class method, you are required to specify 
 `override`, indicating you are redefining existing behavior.

 When implementing an interface method, override *can* be used 
 but doesn't *have to* be used. This seems quite ambiguous and 
 confusing. Am I overriding a method here? If yes, I should be 
 required to use it. If not, I should not be *allowed* to use it.

 Thoughts?
In this case you have just one choice: https://run.dlang.io/is/4zWNED Andrea
Oct 29 2018
parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Monday, 29 October 2018 at 13:44:03 UTC, Andrea Fontana wrote:
 On Monday, 29 October 2018 at 13:14:28 UTC, FeepingCreature 
 wrote:
 When overriding a class method, you are required to specify 
 `override`, indicating you are redefining existing behavior.

 When implementing an interface method, override *can* be used 
 but doesn't *have to* be used. This seems quite ambiguous and 
 confusing. Am I overriding a method here? If yes, I should be 
 required to use it. If not, I should not be *allowed* to use 
 it.

 Thoughts?
In this case you have just one choice: https://run.dlang.io/is/4zWNED Andrea
Indeed, which suggests override should be required for interfaces as well. This also seems to be the result of an informal poll in #d.
Oct 29 2018
parent reply Andrea Fontana <nospam example.com> writes:
On Monday, 29 October 2018 at 14:29:50 UTC, FeepingCreature wrote:
 On Monday, 29 October 2018 at 13:44:03 UTC, Andrea Fontana 
 wrote:
 On Monday, 29 October 2018 at 13:14:28 UTC, FeepingCreature 
 wrote:
 When overriding a class method, you are required to specify 
 `override`, indicating you are redefining existing behavior.

 When implementing an interface method, override *can* be used 
 but doesn't *have to* be used. This seems quite ambiguous and 
 confusing. Am I overriding a method here? If yes, I should be 
 required to use it. If not, I should not be *allowed* to use 
 it.

 Thoughts?
In this case you have just one choice: https://run.dlang.io/is/4zWNED Andrea
Indeed, which suggests override should be required for interfaces as well. This also seems to be the result of an informal poll in #d.
I don't think a poll with three answers is so relevant :) They say it should be required. Anyway if class A : AnInterface { ... } it isn't actually override anything, because AnInterface is just an interface. Class A is just implementing a method. So probably you should not use override if you're not overriding something. Andrea
Oct 29 2018
parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Monday, 29 October 2018 at 14:39:59 UTC, Andrea Fontana wrote:
 I don't think a poll with three answers is so relevant :)

 They say it should be required.

 Anyway if  class A : AnInterface { ... } it isn't actually 
 override anything, because AnInterface is just an interface. 
 Class A is just implementing a method.

 So probably you should not use override if you're not 
 overriding something.

 Andrea
I agree that it's not overriding anything, but it also makes little sense to add a new keyword "implement" that is used as a 1:1 stand-in for override for interface methods, and in any case what would you do in the class/interface case: "override implement void foo()"? And besides, override is *already* permissible for interface implementations, so it already means "override or implement". I'm just asking for some consistency.
Oct 29 2018
parent Andrea Fontana <nospam example.com> writes:
On Monday, 29 October 2018 at 14:47:35 UTC, FeepingCreature wrote:
 I agree that it's not overriding anything, but it also makes 
 little sense to add a new keyword "implement" that is used as a 
 1:1 stand-in for override for interface methods, and in any 
 case what would you do in the class/interface case: "override 
 implement void foo()"? And besides, override is *already* 
 permissible for interface implementations, so it already means 
 "override or implement". I'm just asking for some consistency.
Wait, no. You can simply avoid using override at all for interface. And add it only if you're overriding a class method. Andrea
Oct 29 2018
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/29/18 9:14 AM, FeepingCreature wrote:
 When overriding a class method, you are required to specify `override`, 
 indicating you are redefining existing behavior.
 
 When implementing an interface method, override *can* be used but 
 doesn't *have to* be used. This seems quite ambiguous and confusing. Am 
 I overriding a method here? If yes, I should be required to use it. If 
 not, I should not be *allowed* to use it.
 
 Thoughts?
I think it actually used to be an error (don't remember where it changed, but it was probably an early compiler). If I recall correctly, it was quite a pain to switch between base class or interface because you'd have to go through and remove or add override. What *definitely* changed, is that override used to be optional for base class overrides, but it was changed to be required when you did override. It may have been that the optional behavior was the same for interfaces, but just left alone. For sure, we can't add a requirement for override on an interface, as you aren't exactly overriding an existing implementation. Too much code is out there that does not specify override for interfaces. -Steve
Oct 29 2018
parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Monday, 29 October 2018 at 15:30:27 UTC, Steven Schveighoffer 
wrote:
 For sure, we can't add a requirement for override on an 
 interface, as you aren't exactly overriding an existing 
 implementation. Too much code is out there that does not 
 specify override for interfaces.

 -Steve
Isn't that what the deprecate cycle exists for? I mean, it seems like a symmetrical issue to what you had with regular class override. And the argument that "it's not override on an interface" really seems straight up wrong to me, since you *can* write override - the compiler *already* agrees that implementing an interface method falls under the override umbrella.
Oct 29 2018
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/29/18 11:41 AM, FeepingCreature wrote:
 On Monday, 29 October 2018 at 15:30:27 UTC, Steven Schveighoffer wrote:
 For sure, we can't add a requirement for override on an interface, as 
 you aren't exactly overriding an existing implementation. Too much 
 code is out there that does not specify override for interfaces.
Isn't that what the deprecate cycle exists for? I mean, it seems like a symmetrical issue to what you had with regular class override. And the argument that "it's not override on an interface" really seems straight up wrong to me, since you *can* write override - the compiler *already* agrees that implementing an interface method falls under the override umbrella.
That is something I'm not 100% sure about (the history anyway). I seem to remember that at one point override was an error for interfaces, but maybe that was just what some people wanted to implement. In other words, I think it *was* that way at one point, but the rules were relaxed, but I may be wrong. In any case, I'd prefer a relaxed rule here as there is no hijacking involved for interfaces (which is what the override requirement solves), it will just break valid code for a trivial reason. The case is NOT symmetrical. -Steve
Oct 29 2018