Introduction to Boo

Not too long ago I discovered the existence of a language called Boo.  Boo feels like you are writing in Python but uses the CLR directly and has its own compiler just like C#.  Let’s jump in and take a look at some code to get a feel for it:

import Boo.Lang.Useful;

class MyClass:
    def constructor():
        print “Class was constructed”
    def DoSomething():
        print “Doing something”
print “Hello, World!”

a = “string”
// a = 4 // This would cause a compiler error

as duck = 4
print b
b = “b is a string”
print b

b = MyClass()

From the code you can see that it looks a lot like a chunk of Python code, but you can see some differences. For instance the constructor uses constructor instead of __init__ which is a bit more readable.  Like Python there are no braces and rarely semicolons.  It may seem unimportant but it is actually nice to not have to put in separators to make the compiler happy and format the code so your code reviewers are happy.  The whitespace is significant to the code the same as Python, but in the case of Boo, you can choose any whitespace you want as long as you choose tabs.

The code sample above has some code not associated with the class. One file in your boo project can have code like this and it is combined and put into a main method for you. This is great for easily creating your main method in a console application.

Boo is strongly typed like C#, but you don’t have to explicitly declare the type if the compiler can figure it out.  For instance, the variable “a” clearly needs to be a string.  Once a type is associated, you cannot change it. If you need to declare a type explicitly you can use the “as <type>” syntax.

In addition to leaving out braces and semicolons, you can leave the “new” keyword behind.  If you want to create an instance of a class, just call its constructor. Defining methods on the class also don’t require the explicit parameter self (equivalent of this in C#), that Python has.

There are lots of syntax simplifications you get with Boo. There are areas that make things more verbose though too.  For instance, the “as <type>” could possibly be done w/o using another keyword. Also, I prefer the generic syntax of C# MyClass<string> which is shorter than MyClass[Of string]. It may be possible that the “def” keyword is really not needed to define methods and functions.

Overall, there seems to be a reduction in the amount of required syntax which makes it easy to get a new project up and going quickly.

Duck typing

You may be wondering what kind of type “duck” is from my code sample above. The duck type allows you to have dynamic typing in the same way that Python programmers might expect.  Note that you can change the type of the object you assign to “b” w/o error. In fact, the errors won’t show up until runtime.

Note: You can do this in C# 4.0 using the dynamic keyword and features of C# and .Net framework 4.0. There doesn’t seem to be a lot of documentation out there on this yet, but admittedly, I didn’t look all that hard.

You can support duck typing in your class by supporting the IQuackFoo interface (no I’m not kidding).  With IQuackFoo, how can you not like the language?

Compiler Extensibility

The cool thing about Boo is that the compiler is extensible. There is a pipeline through which the code travels to get compiled into binary form. The first step of the pipeline parses the source file and builds an AST. You can add your own pipeline steps to do transformations on the AST. This has a lot of potential to simplify software development.

You can do some basic exploring of the compiler pipeline by dumping the AST using a sample included with the boo compiler. The sample is in the file examples\


The samples included with the Boo language are a good source. There is also good documentation at the Boo home page. I have also found an interesting book DSLs in Boo which has an overview of the language basics.


In this post I gave some quick pointers to a new language that you may find interesting. But the extensibility of the compiler seems to have a lot of potential to help make development tasks easier. I hope to explore the potential benefits of this in future posts.


Comments (0)

Skip to main content