Closed
Description
I believe C# could benefit from from a feature similar to traits in Scala, or mixins in other languages. This proposal could be a potential implementation for asks in both #73 or #258. The main difference would be in the implementation.
Benefits
- Reduce boilerplate by enabling the definition of default or universal implementations on interfaces.
- Maintain the DRY rule for code that may make heavy use of common interfaces.
Syntax
interface IFoo {
void DoStuff();
void DoThing() => { Console.WriteLine("Did Things"); }
}
Syntax: Overriding default implementation
Goal: Don't introduce new complexity, just reuse "override" here
class Bar : IFoo {
void DoStuff(){
//Do stuff here
}
override void DoThing(){
//My new impl goes here
}
}
This example, while contrived demonstrates a scenario where I've chosen to implement one of the two functions in the interface, For the implementation, I've chosen explicitly to use expression bodied methods as the implementor of the functionality.
Side Note: I wonder how much IEquatable code is being copied and pasted between classes in code bases because this feature is missing.
Activity