Nemerle
Nemerle is a general-purpose, high-level, statically typed programming language designed for platforms using the Common Language Infrastructure. It supports multiple paradigms, including functional, object-oriented, aspect-oriented, reflective, and imperative programming. The language features a simple C#-like syntax and a powerful metaprogramming system. In June 2012, the core Nemerle developers were hired by the Czech software company JetBrains. The team focused on developing Nitra, a framework for implementing existing and new programming languages. Both Nemerle and Nitra appear to have since been abandoned or discontinued by JetBrains; Nitra has not received updates from its original creators since 2017, and Nemerle is now maintained independently by the Russian Software Development Network. However, no major releases have occurred, and development progress remains slow. JetBrains has not referenced Nemerle or Nitra for several years. The language is named after the Archmage Nemmerle, a character in the fantasy novel A Wizard of Earthsea by Ursula K. Le Guin.
Features
Nemerle's most notable feature is the ability to mix styles of programming that are object-oriented and functional. Programs may be structured using object-oriented concepts such as classes and namespaces, while methods can be written in a functional style. Other notable features include:- strong type inference
- a flexible metaprogramming subsystem
- full support for object-oriented programming, in the style of C#, Java, and C++
- full support for functional programming, in the style of ML, OCaml, and Haskell, with these features:
- * higher-order functions
- * pattern matching
- * algebraic types
- * local functions
- * tuples and anonymous types
- * partial application of functions
Type inference
def x = 1; // int
def myList = List; // generic List, type T is deduced from the usage in the next line
myList.Add; // compiler deduces type of T as int making myList type of List
Everything is an expression
def x =
;
def x =
if // if, using, try are also expressions
"Monday"
else
"other day";
def x = try int.Parse
catch ;
def x = returnBlock :
;
Tuples
def k = ; // k :
def = k; // a = 1, b = "one"
Pattern matching
def result = match
Functional types and local functions
using System.Console; // classes and modules can be put in namespaces
def next ; // the type of x argument and other function arguments can be deduced from usage
def mult ;
def fibonacci
WriteLine; // 10 similar to "Console.WriteLine;"
WriteLine; // 4
WriteLine; // 55
Variants
are forms of expressing data of several different kinds:variant RgbColor
Metaprogramming
Nemerle's macro system allows for creating, analysing, and modifying program code during compiling. Macros can be used in the form of a method call or as a new language construct. Many constructs within the language are implemented using macros."if" macro example:
macro @if
syntax ", e1, Optional
// using this macro in code:
def max = if a else b;
// during a compile time the upper line will be transformed to this:
def max = match
Braceless syntax
Similarly to the braceless syntax later added to Scala, Nemerle allows the programmer to optionally use a whitespace-sensitive syntax based on the off-side rule, similarly to Python.The following curly-brace snippet:
using System.Console;
class FooBar
could be rewritten as:
using System.Console;
\
class FooBar
public static Main: void
WriteLine
static Foo : void
if
def y = x * 42;
Foo
else
.Map
static Bar: int
def foo = 2 + 7 * 13
foo
Notably, it is not possible to break expressions or alternative clauses in matches over multiple lines without using a backslash
\:// This will not compile...
static Bar: int
def foo = 2
+ 7
* 13
foo
match
| "a"
| "aa" => 1
| "b"
| "bb" => 2
| _ => 0
// But this will:
static Bar: int
def foo = 2 \
+ 7 \
* 13
foo
match
| "a" \
| "aa" => 1
| "b" \
| "bb" => 2
| _ => 0
In order to activate this syntax, the user must add
#pragma indent to the top of the file or use the compiler option -i.IDE
Nemerle can be integrated into the integrated development environment Visual Studio 2008. It also has a fully free IDE based on Visual Studio 2008 Shell and SharpDevelop.Nemerle can be also integrated into Visual Studio using add-ins and extensions.
Examples
Hello, World!
The traditional Hello World! can be implemented in a more C#-like fashion:class Hello
or more simply:
System.Console.WriteLine;
Examples of macros
Macros allow generating boilerplate code with added static checks performed by the compiler. They reduce the amount of code that must be written by hand, make code generation safer, and allow programs to generate code with compiler checks, while keeping source code relatively small and readable.String formatting
The string formatting macro simplifies variables to string manipulations using $ notation:def s = $"The number is $i"; //insert the value of the variable i where $i is placed
def s = $"$x + $y = $"; // $ can be used to make calculations or access members
Declarative code generation
StructuralEquality, Memoize, json, and with are macros which generate code in compile time. Though some of them can look like C# attributes, during compiling, they will be examined by the compiler and transformed to appropriate code using logic predefined by their macros.// Implement IEquatable.Net interface using by element comparison equality.
class Sample
Database accessibility
Using Nemerle macros for SQL you can write:ExecuteReaderLoop;
instead of
string sql = "SELECT firstname, lastname FROM employee WHERE firstname = :a";
using
and this is not just hiding some operations in a library, but additional work performed by the compiler to understand the query string, the variables used there, and the columns returned from the database. The ExecuteReaderLoop macro will generate code roughly equivalent to what you would have to type manually. Moreover, it connects to the database at compilation time to check that your SQL query really makes sense.
New language constructs
With Nemerle macros you can also introduce some new syntax into the language:macro ReverseFor
syntax
defines a macro introducing the syntax and can be used like
ford print ;
Nemerle with ASP.NET
Nemerle can be either embedded directly into ASP.NET:<%@ Page Language="Nemerle" %>
...Or stored in a separate file and entered with a single line:
<%@ Page Language="Nemerle" Src="test.n" Inherits="Test" %>
PInvoke
Nemerle can take advantage of native platform libraries. The syntax is very similar to C#'s and other.NET languages. Here is the simplest example:using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest