1 day ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | On Saturday, 1 November 2025 at 13:47:22 UTC, Dejan Lekic wrote:
> On Saturday, 1 November 2025 at 06:41:57 UTC, Peter C wrote:
>> ok. now we have the 'accidental implementation' problem.
>>
>
> There is nothing "accidental" there - you have made the decision to inherit from Base, and should know what that means!
Just because you know what it means, doesn't mean I know what it means.
The compiler sure seems to know what it means.
But it's not an absolute that in this situation that the programmer would know what it means - and I specifically use myself as a case study here ;-)
Also, will the next programmer maintaining your code know what it means?
interface Isomething
{
void someMethod();
}
class Base
{
void someMethod() {}
}
class Derived : Base, Isomething
{
// is Derived fullfilling the semantic requirements of interface contract here?
}
| |||
1 day ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Quirin Schroll | On Friday, 31 October 2025 at 23:36:57 UTC, Quirin Schroll wrote:
>
> ...
> This is a good idea for nomenclature in documentation, but it doesn't work with the language. You can implement an interface method by inheriting from a class that defines the method without the base class implementing the interface.
If D had a C# like 'explicit' interface declaration:
module myModule;
@safe:
private:
void main()
{
}
interface Isomething
{
void someMethod();
}
class Base
{
void someMethod()
{
}
}
class Derived : Base, Isomething
{
// Here, I am intentionally binding the interface requirement
// to a concrete method, rather than letting the inherited method automatically
// fulfill the interface contract.
void ISomething.SomeMethod()
{
// This method satisfies the interface, but it's only accessible
// when the object is cast to the ISomething type.
}
}
| |||
1 day ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Peter C | On Sunday, 2 November 2025 at 00:00:35 UTC, Peter C wrote:
> Just because you know what it means, doesn't mean I know what it means.
I am trying very hard to understand you here - you, as developer who wrote the code, deliberately made a decision to inherit from Base class. This means you _do know_ what it means (what that class does).
| |||
1 day ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Peter C | On Sunday, 2 November 2025 at 00:49:36 UTC, Peter C wrote:
> On Friday, 31 October 2025 at 23:36:57 UTC, Quirin Schroll wrote:
>> [...]
>
> If D had a C# like 'explicit' interface declaration:
>
> module myModule;
> @safe:
> private:
>
> void main()
> {
>
> }
>
> interface Isomething
> {
> void someMethod();
> }
>
> class Base
> {
>
> void someMethod()
> {
>
> }
> }
>
> class Derived : Base, Isomething
> {
> // Here, I am intentionally binding the interface requirement
> // to a concrete method, rather than letting the inherited method automatically
> // fulfill the interface contract.
>
> void ISomething.SomeMethod()
> {
> // This method satisfies the interface, but it's only accessible
> // when the object is cast to the ISomething type.
> }
> }
the whole point of it is to not have to cast to interfaces, but just have the type behave a certain way.
just use plain old composition if and alias the methods if you want this.
| |||
17 hours ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | On Sunday, 2 November 2025 at 11:33:56 UTC, Dejan Lekic wrote:
> On Sunday, 2 November 2025 at 00:00:35 UTC, Peter C wrote:
>> Just because you know what it means, doesn't mean I know what it means.
>
> I am trying very hard to understand you here - you, as developer who wrote the code, deliberately made a decision to inherit from Base class. This means you _do know_ what it means (what that class does).
There is no indication to me that Base intended to implement the semantic requirements of the interface. Do you understand that?
And yet, Base.someMethod() is considered an implementation of the interface. I understand that the compiler undertands this (because it has the same signature), but really, the compiler is just taking the easiest possible path here.
It's still not clear to *me* (regardless of the rules here) that by simply inherting a method from Base that has the same signature, that it automatically satisfies the semantic requirements of the interface.
The person maintaining this code will likely ask the same question I did - does Derived implement the semantic requirement of the interface, or not?
A C# like explicit interface declaration in D, would have solved this question straight away. The intent would have been clear, and I would have understood immediately that Derived has indeed implemented the semantic requirement of the interface.
D's starting to remind me of that scene in Ghostbusters, where Peter drives up with (what will become) the ghostbusters vehicle. i.e. look promising, but will need a lot of work.
| |||
9 hours ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Peter C | On Sunday, 2 November 2025 at 23:13:08 UTC, Peter C wrote:
> On Sunday, 2 November 2025 at 11:33:56 UTC, Dejan Lekic wrote:
why do you think Base is considered as an implementation of the interface?
| |||
8 hours ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Serg Gini | On Monday, 3 November 2025 at 07:26:24 UTC, Serg Gini wrote: > On Sunday, 2 November 2025 at 23:13:08 UTC, Peter C wrote: >> On Sunday, 2 November 2025 at 11:33:56 UTC, Dejan Lekic wrote: > > why do you think Base is considered as an implementation of the interface? Actually I've confused myself (and likely others) because I thought this code below actually compiled at one stage (with no error) - turns out... it won't compile. It will produce an error: Error: class `myModule.Derived` interface function `void someMethod()` is not implemented In C#, the code below would actually compile just fine, because in C#, a public method in a base class automatically satisfies an interface method in a derived class if the signatures match. In D, it seems the rule is a little different: A class that claims to implement an interface must itself provide implementations for all interface methods. Inherited methods from a base class are not counted *unless* the compiler can directly see that the derived class provides them (i.e. via an 'override' of the base method). Turns out I prefer the way D *prevents* this from compiling, and actually requires that you 'override' the method in Derived. That signals an explicit action by the programmer to deal with this situation - whereas C# just blindly compiles it. The only way to get the code below to compile in D, is to 'override' someMethod in Derived. But... .. what if I need to keep the semantics of the base class method (or override for some reason), but also implement the semantics of the interface contract? In C# I can do both, by using an 'explicit interface declaration' - which D does not have. I won't spend more time on this, cause it's pretty rare scenario in the work I do, but I've learnt something useful, so wasn't a complete waste of time - for me anyway ;-) btw. Would be interesting to know if anyone in the past has ever suggested C# like explicit interface implementation for D, and what the response was. ................................. module myModule; import std; void main() { Derived d = new Derived(); d.someMethod(); } interface Isomething { void someMethod(); } class Base { void someMethod() { writeln("Hello from Base"); } } class Derived : Base, Isomething { } ................................. | |||
8 hours ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Peter C | On Monday, 3 November 2025 at 08:02:06 UTC, Peter C wrote:
> But...
>
> .. what if I need to keep the semantics of the base class method (or override for some reason), but also implement the semantics of the interface contract?
This is all theoretical - without code example doesn't mean anything.
Drop all prompt that was used before and provide the model name that used for this text generation.
| |||
7 hours ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Serg Gini | On Monday, 3 November 2025 at 08:34:02 UTC, Serg Gini wrote:
> On Monday, 3 November 2025 at 08:02:06 UTC, Peter C wrote:
>> But...
>>
>> .. what if I need to keep the semantics of the base class method (or override for some reason), but also implement the semantics of the interface contract?
>
> This is all theoretical - without code example doesn't mean anything.
> Drop all prompt that was used before and provide the model name that used for this text generation.
'the model name' ??
Anyway...it's solved now. It turns out, that composition was the answer here.
module myModule;
@safe:
private:
import std.stdio : writeln;
interface Isomething
{
void someMethod();
}
class Base
{
void someMethod()
{
writeln("Hello from Base");
}
}
class Derived : Isomething
{
Base base; // composition
this()
{
base = new Base();
}
// Interface implementation
void someMethod()
{
writeln("Hello from Isomething in Derived");
}
// Expose base behavior separately
void callBase()
{
base.someMethod();
}
}
void main()
{
auto d = new Derived();
Isomething i = d;
i.someMethod(); // "Hello from Isomething in Derived"
d.callBase(); // "Hello from Base"
}
| |||
6 hours ago Re: implement vs override | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Peter C | On Monday, 3 November 2025 at 09:32:45 UTC, Peter C wrote:
> On Monday, 3 November 2025 at 08:34:02 UTC, Serg Gini wrote:
>> On Monday, 3 November 2025 at 08:02:06 UTC, Peter C wrote:
> 'the model name' ??
>
> Anyway...it's solved now. It turns out, that composition was the answer here.
So just to finalize for anyone who will read it.
D doesn't have any issues
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply