5 Reasons Why Nim Is Becoming My Favorite Language
Tags: nim, gui, metaprogramming, nimble
Published: 2020-01-08 03:04:59

If you haven't heard of [Nim](https://nim-lang.org/) yet, you're in the majority. It's a very young language that only hit [version 1.0.0](https://github.com/nim-lang/Nim/releases/tag/v1.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](https://www.tiobe.com/tiobe-index/python/):

![Python Tiobe Index](https://thepracticaldev.s3.amazonaws.com/i/c0uyzay1udduzyksngso.png)

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:

``` python
# conditional.py

num = 30

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

Nim:

``` 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](https://electronjs.org/), C libraries like (https://wxwidgets.org/) and [QT](https://www.qt.io/), Java Swing or [JavaFX](https://openjfx.io/). 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](https://nim-lang.org/docs/iup.html) for the [IUP toolkit](https://www.tecgraf.puc-rio.br/iup/), 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](https://thepracticaldev.s3.amazonaws.com/i/z61ftnc15zxpn73grb7r.png)

The Nim team is also working on a separate [UI package](https://github.com/nim-lang/ui) for the standard library based off of [libui](https://github.com/andlabs/libui). Combined with the [several other third-party libraries](https://nimble.directory/search?query=gui) availaible on https://nimble.directory/, you have options! People are already doing cool things in Nim, like making [a NES emulator](https://github.com/def-/nimes)!

![nimes](https://thepracticaldev.s3.amazonaws.com/i/c2fvwyrg25x8ow7iiuy3.PNG)

# 4. A Great Package Manager ([Nimble](https://github.com/nim-lang/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:

``` nim
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:

``` nim
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](https://nim-lang.org/docs/tut3.html) 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:

``` nim
# 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](https://github.com/Vindaar/shell), 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](https://github.com/liquid600pgm/rapid)** get me excited, but I have to stop somewhere! If you're interested in checking out Nim, look at their [official tutorial](https://nim-lang.org/docs/tut1.html). And if you have any questions, leave them in the comments.

Good luck!