5 Reasons Why Nim Is Becoming My Favorite Language

rburmorrison

Ryan Burmeister-Morrison

Posted on January 8, 2020

5 Reasons Why Nim Is Becoming My Favorite Language

If you haven't heard of Nim yet, you're in the majority. It's a very young language that only hit version 1.0.0 late last year. I've been looking at it for a while, however, and with its v1.0.0 milestone, I decided to give it a try. Here are 5 reasons why it might take the place of my favorite language.

1. It's Fully Compiled

The difference between a compiled and an interpreted language can be a thin line. Take Java, for example, in which the code is compiled to bytecode that is then interpreted by the JVM. Nim has no such confusion; it's a fully compiled language with no gimmicks. That makes software that's created in Nim faster and more portable than if it were interpreted. If you want performance metrics, check out this benchmarking repo: https://github.com/kostya/benchmarks.

2. It's "Pythonic"

There's no doubt that Python is incredibly popular right now. Just take a look at its Tiobe index:

Python Tiobe Index

I hear people talk about Python's easy syntax and the ability to prototype quickly with it. If you're one of those people: good news! Python is one of Nim's main inspirations, and it shares a lot of the same syntax. Take a look at the following two snippets.

Python:

# conditional.py

num = 30

if num > 10:
  print("More than 10!")
else:
  print("Less than 10!")

Nim:

# conditional.nim

let num = 30

if num > 10:
  echo("More than 10!")
else:
  echo("Less than 10!")

Not bad, huh? If you have experience with Python, you'll get Nim's basics quickly. There's also a great resource for Python developers that shows the conversions from Python concepts to Nim concepts. You can find that here: https://github.com/nim-lang/Nim/wiki/Nim-for-Python-Programmers.

3. Cross-Platform GUIs

Nowadays, if you want to create a cross-platform GUI, you don't have a whole lot of options. They are: Electron, C libraries like (https://wxwidgets.org/) and QT, Java Swing or JavaFX. Because Nim is built on C and it can use its libraries, it makes using its UI libraries that much easier. Even in Nim's standard library, there's a wrapper for the IUP toolkit, which provides an easy way to create native, cross-platform applications like this:

IUP Example - Pulled From: https://metacpan.org/pod/distribution/IUP/lib/IUP.pod

The Nim team is also working on a separate UI package for the standard library based off of libui. Combined with the several other third-party libraries availaible on https://nimble.directory/, you have options! People are already doing cool things in Nim, like making a NES emulator!

nimes

4. A Great Package Manager (Nimble)

The fun name aside, Nimble does its job well. One of the first things you'll notice about it is the automation that it provides. When initializing a new Nimble application, it will walk users through about 6 quick questions needed to set everything up. Past that, you can just start coding! It makes posting packages to https://nimble.directory/ for the world to see easy and makes your project's dependencies consistent.

5. It Has a Macro System (Metaprogramming)

If you're not familiar with metaprogramming, it can be boiled down to "code that reads and writes code". This means that the code you're writing can manipulate itself and write complex, tedious bits of other code for you that you would otherwise have to do manually. This may seem confusing at first but what if you wanted to create a function called debugPrint that does this:

debugPrint(3 + 3) # output: 3 + 3 => 6
debugPrint(pow(2, 2)) # output: pow(2, 2) => 4

If you can do this without metaprogramming, I'd be thoroughly impressed. In fact, as far as I'm aware, it can't be done. However, if you have a macro system, you can have those lines write themselves. Here's what debugPrint would look like as a Nim macro:

macro debugPrint(expression: untyped): untyped =
  let prefix = newLit(expression.repr & " => ")
  result = quote do:
    echo(`prefix`, `expression`)

Don't get hung up on the syntax. Nim has some good resources to learn how its macro system works, and it's an advanced concept anyway. What you might want to know is that with this macro, the previous example's code gets converted to the following:

# conditional.nim

echo("3 + 3 => ", 3 + 3)
echo("pow(2, 2) => ", pow(2, 2))

Even if you don't ever see yourself using macros, it allows other package developers to create them for you, making your life easier. Take this shell package, for example, which allows you to write shell commands directly in your source code, which then get converted to the appropriate Nim syntax automagically.

Final Thoughts

Look, I could keep going on about Nim's features. Things like easy C interop, Python bridges, machine learning, multiple paradigms, a sub-scripting language and the possibility for game engines get me excited, but I have to stop somewhere! If you're interested in checking out Nim, look at their official tutorial. And if you have any questions, leave them in the comments.

Good luck!

💖 💪 🙅 🚩
rburmorrison
Ryan Burmeister-Morrison

Posted on January 8, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related