Posts

Showing posts from April, 2011

More Macros in Nemerle

After a very brief introduction to Nemerle metaprogramming , what follows is some motivation and better expression macros examples. Historically in C#, the using statement has been abused (guilty!) to provide a more convenient syntax to operations that have natural wrap up code which should be executed at the end of a logical scope. To enable a class to be usable like this, it just needs to implement the System.IDisposable interface. The problem is that these elements were designed with a very specific and important goal in mind: the deterministic disposal of unmanaged resources. That's why using wraps the call to IDisposable.Dispose() in a finally block: it has to run no matter what. A finally block is a very strong statement. It runs in the presence of exceptions, and it can't be interrupted by a ThreadAbortException , what can delay a whole application domain shutdown. That's justified for releasing resources, but it is too strong a statement for simple wrap...

Metaprogramming in Nemerle

Nemerle is a generally unknown .NET language that nevertheless has many impressive features. Its syntax is close to C#, but it has many functional programming constructs, like algebraic data types and pattern matching, that are not found in the Redmond language. It also has a very powerful metaprogramming system, where special constructs can participate in the compiler's execution pipeline and generate or manipulate code in the target program. Metaprogramming can be very useful to cut on duplication, to apply consistent policies throughout a codebase and to raise the overall expressiveness of the code. It's one of the things that makes Ruby on Rails such a compelling and successful web application framework. Unlike Ruby , Nemerle is a compiled, statically typed language, where metaprogramming is possible already in the compilation step. For a very simple (and unrealistic) example, imagine that we use lots of infinite loops in our code. In some parts we use while(true) ...