A Programming Language Underdog
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 |
|
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.
Official Things | |
Web Site | The project’s entry point |
Source | The github project |
nimble | The nim package manager |
choosenim | Toolchain installer |
Community | |
Forums | An async discussion board |
On Twitter | Nim’s Twitter account |
On Reddit | A subreddit community |
IRC | #nim on irc.freenode.net |
Learning | |
Documentation | Links to documentation and tutorials |
Nim In Action | A book published by Manning Press |
Intro Video | Dominik Picheta gives an overview |
Discovery | |
nimble directory | Web front-end for nimble |
Awesome Nim | Curated projects (awesome style) |
Github Projects | open source project; sorted by recently update |
Editors | |
Vim Plugin | Nim rhymes with Vim |
VS Code Plugin | Visual Studio Code plugin |
Go Write Some Apps | |
Jester | Write some web services |
Karax | Write some single page web apps |
NiGui | Write GTK+ 3 / Win32 desktop apps |
Winim, wNim | Write Windows apps |
objc | Write macOS apps (wip) |
android | Write Android apps |
Arraymancer | Write deep learning apps |
serial.nim | Write serial-port apps |
libusb | Write USB apps |
Ethereum 2.0 | Ethereum .. uh.. things - (seriously was my best attempt) |
That’s a fun start, but there is so much more. I’ll get to work on some more articles.