Default Interface Methods should be callable from implementing types #7227
Replies: 5 comments 6 replies
-
In your 1st example, interface I { string Foo() => "foo"; }
class C : I
{
public string Foo {
get {
//Do some extra business logic here
//Now delegate to I's implementation of Foo
return ((I)this).Foo(); // It works well
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Wasn't this supposed to be a part of a phase 2 for DIM that included a syntax like |
Beta Was this translation helpful? Give feedback.
-
Thanks @MgSam for bringing this to the forefront and confirming that it is still important! Your scenario is certainly compelling to me. It turns out that there is an issue for this on the backlog: #2337. I've added myself as a champion, and will put it on the LDM agenda to triage - i.e., see what the interest level is. If they agree it's time to look at it again, let's take a look at implementation complexity; it sounds like there may have been some beneficial changes since this was first proposed. |
Beta Was this translation helpful? Give feedback.
-
This is also a feature that I was really looking forward to. I implemented it myself about 1 year ago cause I had to use it asap.
I hope these will be useful until the C# team implements the feature, |
Beta Was this translation helpful? Give feedback.
-
Agree that this is badly needed, since could be a massive source of bugs in years to come when interfaces are used without detailed knowledge of their implementation! I'll go and follow the issue in the backlog too. |
Beta Was this translation helpful? Give feedback.
-
Currently, Default Interface Methods cannot be called directly from an implementing type.
This seriously limits the usefulness of DIMs. It means that what you put inside them always has to itself delegate to another method or else you have siloed logic in a method that cannot be useful to any of its implementers.
As a motivating example in my own usage, I have a framework where we have abstract base classes that do very little and would be better served replacing them with interfaces (so users are not forced into our inheritance hierarchy). But the small amount of logic they do have would be lost if its put in DIMs as implementers have no way to call it.
You can sort-of workaround this situation today using statics. But doing so is extremely clunky and repetitive. It's also such a strange looking pattern that I am hesitant to use it as future maintainers of the code may not understand the reason for it and remove it.
I propose to implement the syntax that was in the draft solution for C#:
Beta Was this translation helpful? Give feedback.
All reactions