Introduction:
Modern object-oriented programming languages provide, at their most basic level abstraction (through interfaces), encapsulation (through private class members) , programming by difference (through inheritance/decoration), and composition (of data through containers and of logic through declaring sequencial operations) . These 4 concepts give us the power to divide a complex problem into more bite-sized chunks, providing knowledge barriers in order to hide information unrelated to the bite-sized piece the developer is working on at the time.Modern languages also inherit some very important but non-object-oriented abstractions too. This is because some abstractions we would like to create do not naturally fit into the object-oriented model where data and logic are encapsulated together. Some examples are :
- Purely functional abstractions (like mathematical operators)
- Pure data abstractions (like DTO's)
- Memory Management
- Threading issues like deferred execution, locking, asynchronous calls
- Aspect oriented programming / cross-cutting concerns
The more language features we include, the more complex the language becomes. This is why most popular languages only implement the features that increase productivity, and leave out the ones that create more reliable code. Besides, some of the abstractions can be massaged into an object oriented framework or library. The only problem with using the OO counterparts is:
1) The syntax does not have a natural language type flow
2) There is no compile time checking
3) They often rely heavily on reflection.. a feature which typically has costly performance overheads
This article explores the concept of giving the developer the ability to pick and choose language features, or even create new language features specific to their domain.
The way we do things currently
From the application developers point of view, the language he uses should be everything he needs to express the requirements in a non-ambiguous way. This is achieved by providing language layers, of which only the bottom layer is the actual programming language. Each layer can function as an extension of the layer below it, or it could hide the details of the layer below it. The former case is similar to inheritance, the later similar to encapsulation.Here is an example of a typical web services application and it's layers
| Application wiring (often represented with a diagram, or as a IOC-configuration file or web.config) | |
| Features (The part that most closely represents the requirements specification) | |
| Reuseable framework and library components | Domain specific classes (encapsulate the business objects) |
| Compiler language | |
The way of the future?
I would like to suggest another layer just above the "Compiler language". I think the compiler language should be very simple and support very few features. Just above this layer should be a set of features that are part of the compilation process, providing language elements that are executed and verified at compile time.In the same way we include namespaces in a class header file we should be able to import just the language features that are needed . This could be defined at the scope of the application, or more finely scoped to the namespace/class level. If we were able to do this, then the base language would be able to support any number of arbitrary language features, even ones that are mutually exclusive of each other.
One thing is for sure. If we had the power to create our own language features, we wouldn't be waiting for microsoft to bring out their next release of .Net before we start using generics(v2.0), link(v3.5), or dynamic types(v4.0)! It would also be very nice to not have to develop your modules in Java just because you want checked exceptions, or Eiffel just because you want design-by-contract.
So what are the roadblocks to providing this kind of extensibility? We have been creating applications that are reliable and extendable by using good OO techniques and test driven development for years. We can add modules in small units and verify the application works as expected. Is it too hard to apply the same principles to compiler design?
I'm looking for some feedback on that question.
The base-level-compiler would need to provide only a few language features, plus hookins for the feature extensions. The compiler could be re-compiled and retested with each new feature added, or it could provide a plugin type architecture.
Concrete examples of things you could do with an extensible compiler:
- Language features to support state patterns, and then on top of that syntax like: until (some event) do {...}
- Being able to extend the metadata of a class without modifying the class's definition.
- Many frameworks and toolkits that use reflection , dynamic code generation, or code weaving can be re-written to do their work at compile time.
Roadblocks in C#
It seems that in C# many language features are hacks where the compiler needs explicit info on class definitions in order to do lots of "special case" compilation. For example [AttributeUsage] and [ThreadStatic] attributes. The compiler has specific knowledge of these attributes, which shows a dependency of the compiler on a compiled MSIL library??!! Other examples are :- foreach/yield -> depends on IEnumerable
- using -> depends on IDisposable
- try/catch -> depends on Exception
- linq -> depends on IQueryable
- dynamic -> depends on DynamicObject