Boo extensions explained
Boo’s compiler extensibility means that you can write extensions that run in the compiler when your Boo code compiles. This powerful feature enables you to actually extend the Boo language itself. In this post I will try to explain what this basically means.
Say you want to make a new keyword in Boo, called “WriteHelloWorld” (yes, it’s an extremely bad example, but I want to keep it as simple as possible). Boo does not understand the word “WriteHelloWorld” by default, but you can make it understand. What you have to do is write a little “plug-in” to the compiler.
The “WriteHelloWorld” plug-in have to be implemented in a separate assembly from the assembly that is using the keyword. This is because the plug-in assembly with the “WriteHelloWorld”-functionality has to be run in the compiler and thus already be compiled during the compilation of your main assembly. I’ve added an illustration that I use in some of my Boo-presentations.
When your main assembly is under compilation, the “WriteHelloWorld”-plug-in will be asked for help every time the compiler hits the “WriteHelloWorld”-keyword.
I’ve implemented the WriteHelloWorld as a meta method, since it is the easiest way to create new keywords if you do not need to work with the ast-tree.
1 2 3 4 5 | [Meta] static def WriteHelloWorld(): return [| Console.WriteLine("Hello World!") |] |
This method tells the compiler that when it finds the “WriteHelloWorld” keyword, it should replace it with the code “Console.Writeline(”Hello World!”). And it does, if you look at the output assembly (using a tool like Reflector), you will see that this is exactly what the Boo-compiler has written. Remember that this is compile-time. Within the compiler process itself. The WriteHelloWorld-metamethod will never be executed run-time, it won’t exist after compilation.
Notice also that the main assembly has to reference the plug-in-assembly. If you do not do that, the compiler does not know where to find the plug-in.
This is how all extensions/plug-ins work in Boo. The compiler asks a plug-in to interpret what a keyword/attribute means, and gives the plug-in control of what to do. If you use Macros or Attributes you will also be able to access the compiler-tree, enabling you to manipulate the whole compiler process. For even more power you can add custom steps to the compiler pipeline, where you can do literally anything you want to with the code. More about that another time.
I’ve attached the solution that implements this. Read Getting Started to see what you need to run the solution.
Boo extensions explained Solution
- Tore Vestues


[...] Boo extensions explained/ [...]
December 18th, 2008 at 0:32 (064)[...] Boo extensions explained [...]
December 22nd, 2008 at 23:25 (017)