What I like about the Nimrod programming language

Nimrod is a relatively new programming language which I find very interesting mainly because it is a compiled language and has a syntax which is a combination of Python’s and Pascal’s syntax, the two languages I find most aesthetically pleasing, with a small sprinkling of other influences, like JavaScript.

The language and its community are growing, there is an increasing number of available modules (like Jester, for web apps), and it’s currently on the top of my list of things to learn.


I haven’t yet really started learning Nimrod (i.e. I haven’t started a project using it…) but from the sources of a forum web-app I can already say that there are a lot of things I will like about it:

Overall, I think Nimrod is more interesting language than Go and other languages which have received significant publicity in the past several years.

Here are some examples of Nimrod code with comments what I like about them:

type
  TMsgKind = enum
    mLine, mEof
  TMsg = object
    case k: TMsgKind
    of mEof: nil
    of mLine: data: string

I simply think this is gorgeous syntax! Of course, this is personal opinions and others may disagree, but I like words instead of sigils in my code.

It also illustrates:

  • Similarity to Pascal: the “type” keyword, classes starting with uppercase T as a convention, not a requirement, “variant” classes (the “case” part) as a syntactic sugar for a concpt similar to C unions, but which is enforced and less haphazard
  • native enum support
  • native string type

Here’s another type definition:

type
  TVector {.bycopy, pure.} = object
    x, y, z: float

Nimrod has a special “pragma” syntax - the “{. … .}” string which can modify various properties of types and variables. These are similar to standard and compiler-specific notations such as “volatile” or “__attribute__ ((aligned (16)))”. In the above case, the class is marked as pass-by-value. Normally, similarly to other modern languages, only simple scalars are passed by value and objects are passed by reference, but the “bycopy” and “pure” attributes specify the objects will be passed by value by default.

Some of the pragmas (and there are a lot of them!) are frankly surprising. Apparently, there’s a pragma for automatic loop unrolling!

proc searchChar(s: string, c: char): int =
  for i in 0 .. s.high:
    {.unroll: 4.}
    if s[i] == c: return i
  result = -1

It will be interesting to see how this plays out IRL. Also note the special variable “result” which comes from Pascal and implicitly holds the result value of the function (there is also an optional “return” statement).

Pragmas are not the only surprising thing Nimrod supports. It supports term rewriting for the AST of the program to be compiled with templates, using pattern matching! See here: 

template optMul{`*`(a, 2)}(a: int): int = a+a

This piece of code redefines the operation of multiplying an “int” with a “2” to be replaced by adding the int to itself - i.e. this will replace all code of the form “x*2” with “x+x” in the AST before compilation, which is stuff normally bragged about by LISP-ers and some functional programming fans! Templates are “hygienic” with regards to declaring local identifiers.

It would be difficult to present all the features Nimrod has, and for me, this find is simply the best thing I’ve found in recent years! Check it out!


#1 Re: What I like about the Nimrod programming language

Added on 2013-10-13T22:20 by Andrew
Nice see post about nimrod, it is a really nice and cool language but lacks of documentation and a prolific community, I’ve this in my list, I hope gain interest and popularity, personally I consider go a really weak language only “popular” by the Google brand

#2 Re: What I like about the Nimrod programming language

Added on 2014-03-06T13:30 by Somebody

Go is about language tools and ecosystem not about the language itself. Also the concurrency as first-class is another plus to Go.

About Nimrod I am really skepticabout template/macro + identation based blocking.

One nice thing about the language not metioned in the article is the GC:

A fast non-tracing garbage collector that supports soft real-time systems (like games).”

#3 Re: What I like about the Nimrod programming language

Added on 2014-03-15T21:53 by seyko2
template optMul{`*`(a, 2)}(a: int): int = a+a
will replace all code of the form "x*2" ...
How about code 2*x ? It will not be replaced?

About Go... I think the concurrency can not be turned off in it.
And this is a big issue :-) Concurency in nimrod is more controlled by programmer
(I suppose)
Like this article? Thank me with some Doge at DKUKYfhKV6bT77rkv9oLViTp5N9F3VtHXY
or BTC at 1MstKbw3XjeG3zE9biKyZ2XKADouuYfHAa!

Comments !

blogroll

social