Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Nemerle: A Hybrid Programming Language For The .NET Platform

Nemerle: A Hybrid Programming Language For The .NET Platform

Nemerle provides the ability to mix object-oriented and funtional programming techniques which can be quite handy in some problem domains.  The top-level program structure is OO but in the body of the program you can use (but are not limited to) a more funtional style.  Functional values, variants and pattern matching are some of the features that enable this. 

Functional Values
Functional values allow you to pass functions as arguments of other functions as well as pass them as a return value.  They are similar in nature to C# delegates.

Variants are similar to the C# enum.  The easiest way to demonstrate this is by example:

variant RgbColor {    | Red    | Yellow    | Green    | Different {        red : float;        green : float;        blue : float;      }  } 

In this case, if the color is not red, yellow or green, it can be represented by RGB values. 

Pattern Matching
Pattern matching is described as being similar to the C# switch statement on steroids and is used to simplify working with variants.  There are many types of patterns offered and you can do some powerful things with them.  Check out this section of the Grokking Nemerle tutorial for examples of their use.

Other Important Features
Other features in Nemerle are intended to simplity the life of the programmer.  This is where macros and type inference fit in.

Macros in Nemerle are much more than boiler-plate code expansions.  Macros in Nemerle offer code generation with additional static checks by the compiler.  One compelling example is the use of the SQL macros.  With them you can write:

ExecuteReaderLoop ("SELECT firstname, lastname FROM employee WHERE firstname = $myparm", dbcon, {   System.Console.WriteLine ("Name: {0} {1}", firstname, lastname)  }); 

 Instead of:

string sql = "SELECT firstname, lastname FROM employee WHERE firstname = :a"; NpgsqlCommand dbcmd = new NpgsqlCommand (sql, dbcon, dbtran); dbcmd.Parameters.Add("a", myparm);   NpgsqlReader reader = dbcmd.ExecuteReader();   while(reader.Read()) {   string firstname = reader.GetString (0);   string lastname = reader.GetString (1);   System.Console.WriteLine ("Name: {0} {1}", firstname, lastname)  } reader.Close(); dbcmd.Dispose(); 

The macro doesn't simply hide functionality away in a library.  Work is performed by the compiler to evaluate the query string, variables used, and the columns that will be returned by the query.  It will also connect to the database at compile time to verify that your query actually makes any sense.

Type Inference
Ruby afficianados will no doubt understand this concept already.  Why declare the type of your variables when in many, if not most, cases the compiler can figure out (infer) the types. A simple example will make help to make it clear.

Why write all of this: Dictionary<string, int> d = new Dictionary<string, int> d.Add ("Ala", 7); foreach (string s in args) {   ... }  When this will suffice:  def d = Dictionary (); d.Add ("Ala", 7); foreach (s in args) {   ... } 

Nemerle is certainly something to keep an eye on.


Rate this Article