Mixin Support Via Compiler Generated Interface Implementation - by otac0n

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 627979 Comments
Status Closed Workarounds
Type Suggestion Repros 0
Opened 12/3/2010 8:24:16 AM
Access Restriction Public


This was originally suggested by Jon Skeet on StackOverflow.com here:

Basically, the feature requested is a keyword 'implements' that would auto-generate a pass-through explicit interface implementation for a given interface, referencing a given member variable.

For example:

// Here is the Mixin Interface
public interface IAge
    TimeSpan Age { get; }
    void SetBirthDate(DateTime birthDate);

// Here is the concrete implementation of the Mixin.
public class AgeProvider : IAge
    private DateTime birthDate = DateTime.UtcNow;
    public TimeSpan Age { get { return DateTime.UtcNow - this.birthDate; } }
    public void SetBirthDate(DateTime birthDate) { this.birthDate = birthDate; }

// This is a base class for our target class
public abstract class Animal
    // Animals might not have an "Age" because they don't care about the Gregorian calendar.

public class Human : Animal, IAge
    private AgeProvider ageProvider /* implements IAge */ = new AgeProvider();
    // The implements keyword above would provide this style of implementation:
    TimeSpan IAge.Age
        get { return ((IAge)this.ageProvider).Age; }
    void IAge.SetBirthDate(DateTime birthDate)
Sign in to post a comment.
Posted by G1 on 6/22/2011 at 12:55 PM

I disagree...

Interface implementation through property/field delegation is a sufficiently valuable feature to be included in the language, indepedent of any allusions to mixins. The absence of this feature in interface oriented single inhertance languages such as C# and VB.Net is a sorry oversight. As a result, their software text becomes ungainly verbose. Consequently, the possibility of succinct straightforward elegant code is lost, or at least becomes unreasonabley difficult to attain and maintain.
Posted by Mads [MSFT] on 2/24/2011 at 1:20 PM
Thanks for your suggestion!

I've seen this a few times - indeed from Jon Skeet himself as well.

If we are ever to implement mixins I am not in favor of doing it this way. The problem is that the mixed-in behavior - or rather the object that it sits on - has no knowledge of the object it is getting mixed into. It can't for instance say "I'm a mixin for Animals" and then use Animal methods from the eventually-mixed-into-object in implementing its own methods. Of course you could wire something up, but then it starts getting complicated.

This feels to me a little too much as a half-hearted glue-on. I'd rather do mixins properly and deeply integrated in the language. Scala's traits in my opinion provides one of the best model for this. But I don't see us going there anytime soon!

Thanks again,

Mads Torgersen, C# Language PM
Posted by José Romaniello on 1/24/2011 at 2:40 PM
Hey, I am working on a Mixin weaver for .Net Framework.. using Mono.Cecil and a msbuild post build task.
This is not a workaround yet but I am very interested in any feedback you can provide.
The project is here: http://code.google.com/p/heredar/

thank you
Posted by Microsoft on 12/3/2010 at 9:21 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)