Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zoalasaurusrawr opened this issue Dec 29, 2016 · 6 comments
Closed

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

zoalasaurusrawr opened this issue Dec 29, 2016 · 6 comments
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue

Comments

@zoalasaurusrawr
Copy link

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.

@iam3yal
Copy link

iam3yal commented Dec 29, 2016

@wmccullough There are multiple requests about it and I guess that there are more out there. . :)

#16074
#8127
#60

@zoalasaurusrawr
Copy link
Author

Thank you, believe it or not I did search this beforehand and had little luck, perhaps because the terms are all used a hundred times in different situations.

@alrz
Copy link
Member

alrz commented Dec 29, 2016

This is actually more related to #258.

@gafter
Copy link
Member

gafter commented Dec 30, 2016

This appears to be a dup of #258.

@gafter gafter added the Resolution-Duplicate The described behavior is tracked in another issue label Dec 30, 2016
@aluanhaddad
Copy link

aluanhaddad commented Dec 31, 2016

Let's not forget how this relates to #4586 :) Scala has the with keyword which composes traits.
I saw a talk where Martin Odersky said they are replacing with a more precise & (with isn't recursive but & will be)

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

Traits are now proposed at dotnet/csharplang#52 and are under active design/development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

6 participants