A Programming Language Underdog September 20, 2018

As devs in late 2018, we’ve got lots of programming language choices. Mainstream ones alone, there’s about a dozen or so of them. They’re all awesome. As you wander a bit further down the obscurity path, you’ll find languages like elm , elixir , purescript , and reason . And just past those, you’ll find nim .

I found out about nim by accident.

I was looking at a library for ui development and it said, “We also have bindings for go , rust , nim and others.” Cool… wait. What the hell is nim ? Out of curiosity, I gave it a try.

A couple of throw-away examples. A rewrite of an old project. A few new prototypes of incredibly cringey ideas. You know, mostly trying to get a sense of where the edges are.

5 months later, this language has still got me captivated.

First, A Speed Run

We’re going to make a small CLI app so we have something to talk about for the rest of this article. So let’s install nim and get a feel for the toolchain.

Start by mkdir fun , then cd fun , and create a text file called wave.nim with this nonsense:

# this is wave.nim const hand * = "👋"

Create another file called hello.nim and paste this in:

# this is hello.nim import wave echo "Hello World " & hand

Now let’s compile!

$ nim c hello.nim

About 175 ms on my laptop running Ubuntu. Let’s run it.

$ ./hello Hello World 👋

You have a binary that is fast ( 2 ms ), small ( 107 kB ) and dependency-free.

A Crafty and Capable Compiler

We fed the compiler the entry point ( hello.nim ) and it found its dependencies ( wave.nim ) by examining the code. If we were to drop a 3rd file in there, well, it wouldn’t have included it if we didn’t use an import statement.

We didn’t give it a main() function, and it just made an executable!

But that’s not the only option. Run nim c --app:lib wave.nim and we just made a dynamic library. Run nim c --app:staticLib wave.nim and now it’s a static library.

Windows, macOS, Linux are supported, as are a few other platforms like Raspberry Pi and Nintendo Switch! With the right setup, cross-compilation can be a thing, but the story is nowhere near what go can do. Honestly, nothing I’ve seen touches go in the build arena.

A similar c program is only 8.5 kB

nim bakes in a bunch of functions and constants including file I/O and a configurable garbage collector which adds overhead.

This ain’t your grandfather’s garbage collector. You can pick the algorithm (6 choices) and the time budget per activation. And it doesn’t run “whenever” like most do. You can even turn it off if you live in YOLOville.

Also, we compiled in debug mode, lol. In debug mode you get fancy stack traces and other things to help you, well, debug. Try compiling with nim c -d:release hello.nim . Down to 89 kB . Better?

With another compiler flag, you can optimize for size instead of speed. nim c -d:release --opt:size hello.nim and now we’re 44 kB . Better.

But the c compiler is faster!

Ya, that’s true because nim compiles to c! Then it compiles to a binary by using gcc or clang (for example).

Remember when we typed nim c hello.nim to compile? The c stands for compile to c and it’s the default backend.

It can compile to c++ and obj-c too.

Then we can use the tools of the backend’s ecosystem (like code coverage and debuggers). Seriously.

So good language interop?

You bet. That’s one of the strengths.

Use libs like imgui , SDL , redis , you name it. Use static libraries or link to dynamic ones like openssl . There are language features to deal with name-mangling, namespacing, linking, and more. Hell, you can even type c code in a string within nim and the compiler will do the right thing. There’s also a c2nim tool for helping you automate wrappers.

So Cross Platform / Cross Language? That’s crazy!

No, that’s awesome! Crazy would be compiling to JavaScript. 😂

$ nim js -d:release hello.nim $ node nimcache/hello.js Hello World 👋

Ok then, it compiles to JavaScript.

Why Haven’t I Heard Of nim Before?

Here’s one reason:

Language Funded By rust Mozilla swift Apple go Google typescript Microsoft kotlin Jetbrains nim _________

The correct answer, of course, is 🤷.

With big funding comes big reach and big communities. Although nim hasn’t had that kind of sponsorship, it looks to be heating up.

What I find amazing is that despite the grassroots nature of this project, it has features that run alongside the current mainstream leaders.

Learn More

Here’s a big ol’ stack of URLs to get going. I’ve also heard Google is a pretty good world wide web site too.

That’s a fun start, but there is so much more. I’ll get to work on some more articles.