BT

Nemerle: A Hybrid Programming Language For The .NET Platform

by David Totzke on Jun 07, 2006 |

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
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
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.

 

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT