16 hours 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?
}
| |||
15 hours 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.
}
}
| |||
5 hours 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).
| |||
4 hours 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply