Tuesday, April 01, 2008

Cola Interface Support
Finished basic support for interfaces today. interfaces are limited to single inheritance for now, will have to fix that to fully expose the .NET builtin libraries.

Interfaces are key for running on .NET and Java due to their heavy use in the standard libraries, as well as the integration into the languages. The foreach() statement was originally implemented in C# 1.0 to require the collection being iterated to implement the IEnumerable interface, but recently I noticed that C# in VS 2005 only checks for the required method signatures, not the actual interface. I'll probably follow suit with Cola.
interface IEmpty {}

interface IHasMethods {
   void test();
}

class Class {}

class ClassChild : Class
{
}

class ClassWithInterface : IHasMethods
{
}

This is typical syntax but I took a different approach from Mono C#, or at least the old grammar I have looked at. Where Mono implements interfaces explicitly in the parser grammar, I am sharing the grammar with the class rules, and checking interfaces during the semantic phase. This just means colac will parse some illegal syntaxes, but emit errors when it finds the illegal constructs, such as declaring a field inside an interface, or declaring a method with a body.

Time will tell whether this approach works better, I favor it because it would seem to allow better error reporting than implementing in the parser layer only, which often results in a cryptic "syntax error". It does require a bit more special case code in the semantic checker, but at least the errors have more context.

2 comments:

Unknown said...

Will you support multiple inheritance for classes as well as interfaces?

mrjoltcola said...

Not planning on doing multiple inheritance. Makes the compiler implementation much simpler, plus I rarely use it even as a C++ programmer. I intend to focus on productivity features, and multiple inheritance is often a feature that causes more decision for me than anything else.