Feature Request: Static Interfaces
I want C# 3 to have support for static interfaces! What is a static interface?
A static interface is like a normal interface which a class can implement, but it may contain static methods and constructors. The syntax is simple, just add the static keyword on the methods that you want to be static, add constructors as usual and you're set (I've bolded the new parts):
public interface IDocument
{
void Save();
void SaveAs(string name);
IDocument(IDataStore dataStore);
static IDocument Create();
static IDocument Open(string name);
}
public class DocumentImpl : IDocument
{
DocumentImpl(IDataStore dataStore) { ... }
void Save() { ... }
void SaveAs( string name ) { ... }
static IDocument Create() { ... }
static IDocument Open(string name) { ... }
}
A class that implements IDocumentCreator must provide static implementation for the Create() and Open() methods. In this case the problem is relatively simple, but in many cases such a thing can be a really big problem in trying to design a class hierarchy where static methods and constructors are part of the interface.
I'm sure that most developers can account for at least one case where they really wanted a way to specify either constructors or static methods in an interface, because that would simplify their life so much. The current solutions are factories [or factories' factories (and so on... )] which suffer from other set of problems or using reflection. Both of solutions are half measures, since it's a way to try to bypass the language limitation and gets the above code to work.
How would I use the above code? Idealy, like this:
public DispalyNewDocument(static IDocument docType)
{
IDocument doc = docType.Create();
IDocument doc2 = new docType(dataStore);
//Dispaly docs.
}
DisplayNewDocument(DocumentImpl);
How would the compiler implement such a thing? Well, for a start, it would seperate the IDocument interface into two parts a static one and a regular one, the static one would result in an automatically generated class, which would implement the methods mark as static as instance methods with simple delegation to the static ones. Everywhere where I need the static portion of the interface, I just use "static InterfaceName" and pass the name of a class. The compiler would then pass an instance of the generated class. The compiler should also be smart enough to turn calls to new InterfaceInstance( args ) to method call to the interface (called Constructor, naturally) which would then return the instance of the class, simply by calling new on it.
The code declaration that I've above should generate the following:
public interface IDocument
{
void Save();
void SaveAs(string name);
}
public interface IDocument_StaticPortion
{
IDocument Constructor(IDataStore dataStore);
IDocument Create();
IDocument Open(string name);
}
public class DocumentImpl : IDocument
{
DocumentImpl(IDataStore dataStore) { ... }
void Save() { ... }
void SaveAs( string name ) { ... }
static IDocument Create() { ... }
static IDocument Open(string name) { ... }
}
public class DocumentImpl_StaticInterfaceImpl : IDocument_StaticPortion
{
public IDocument Construcotr(IDataStore dataStore)
{
return new DocumentImpl(dataStore);
}
public IDocument Create()
{
return DocumentImpl.Create();
}
public IDocument Open(string name)
{
return DocumentImpl.Open(name);
}
}
And the code that is using it should generated:
public DispalyNewDocument(IDocument_StaticPortion docType)
{
IDocument doc = docType.Create();
IDocument doc2 = docType.Constructor(dataStore);
//Dispaly docs.
}
DisplayNewDocument(new DocumentImpl_StaticInterfaceImpl());
Using fairly simple transformation and no changes to the CLR itself, we get an incredible boost to productivity via static interfaces. We can now specify all the details about our class, including the static ones. This has a lot of implication on design and totally eradicate a large class of creational patterns, since you can just express them fully in the language.
What do you think?
Comments
Comment preview