Linked by Eugenia Loli-Queru on Sun 5th Nov 2006 22:59 UTC
Thread beginning with comment 179277
To view parent comment, click here.
To read all comments associated with this story, please click here.
To view parent comment, click here.
To read all comments associated with this story, please click here.





Member since:
2005-07-06
This is used in languages like Scala (where it's called a "trait"), and there was talk of using it in C#.
Basically, think of it as a partially implemented interface. Implementations cannot refer to any variables, they can only refer to things declared in the interface itself (i.e. methods and properties). This helps avoid the diamond problem that occurs with traditional multiple inheritance.
As it turns out, this is a good thing. When a framework designer is creating an interface, they have to accommodate two groups, those who have to implement the interface (who will want the minimum number of methods) and those who will use the interface (who will want a lot of convenience methods). Traits (aka partially implemented interfaces, aka concrete protocols) help provide this: consider this hypothetical declaration of a list interface
public interface IList<T> {
public int numElements
{ get;
set;
}
public T this (int pos)
{ get;
set;
}
public void addElement (T item);
public void removeElement (T item);
public boolean isEmpty()
{ return numElements == 0;
}
}
This provides the useful isEmpty() method for interface users while relieving interface implementators of the job of implementing this (though they may if they want).
As regards the issue of multiple competing implementations, as far as I know, Scala uses the implementation in the last interface in the list.
Note, as far as I know, C# 3 will not support this and I'm pretty convinced I've made more mistakes, C# not being a language in which I'm 100% fluent.
Edited 2006-11-06 17:40