January 17, 2021

Why Nim?

About a year or so ago I came across a promgramming language that I’ve never heard of before called nim which intregued me. It’s a statically compiled systems language. At first glance it looks pythonic.

import strformat

  Person = object
    name: string
    age: Natural # Ensures the age is positive

let people = [
  Person(name: "John", age: 45),
  Person(name: "Kate", age: 30)

for person in people:
  # Type-safe string interpolation,
  # evaluated at compile time.
  echo(fmt"{person.name} is {person.age} years old")

And it’s ergonomics are pretty pythonic too, but that’s where the comparisons stop. In fact it’s more like ada or pascal with it’s typing and procedrues.

However the ergonomics plus the promises for performance that could be compiled into a single static binary is what drew me in. Thus when people see this there are a few questions and comparisons on why this over rust or go?

Here’s my shortlist of the what and whys I decided to take a stab at investing more time into learning nim.

1. Ergonomics

For me ergonomics in a language matter a lot and what makes it fun. If I am spening a ton of time programming I desire less time writing boilerplate, and something that is easy to read. For me being easy to read deals with both tearseness and clarity both of which I have found in nim comapred to something say like rust. The GC also helps to not worry about memory management but it is easy enough to disable and do manual memory management without being considered dangerous. In regards to go it can read like psuedocode which is more inuitve. Additionally since it has static typing, is strongly-typed and compile time checks there is a better chance you don’t shoot yourself in the foot which is easier than python.

In short it’s a compiled language that feels a whole lot like a scripting language.

2. Performance

Even with the feel of a scripting language it doesn’t sacrifice performance at all so you can write some highly performant code. Here are some benchmarks.

3. Metaprogamming

Nim allows you to treat code and programs as data so you can build your own macro system to create code to generate code. With generics you can also cut down on reptative code.

4. Packaging

This really belongs in ergonomics but the package system is a whole lot better than say golang even though it’s gotten better with modules. I’d say that nimble is quite compariable to cargo in lots of ways.

There’s a bunch of other features that nim has that makes it a joy to work with. Of course other languages have some of these features but by having them all together you get a powerful language. There is no subsitute to trying it out yourself so hopefully this was enough for you to try and see if nim is worth reading more about or to install and try out.