Skip to content

Proposal: Partial interface implementation a.k.a. Traits #16139

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions