23rd February 2018 - Nim

Nim recently had a stand at FOSDEM 2018. It was lot's of fun meeting up with all the people whom I've only gotten to know by their IRC nicknames. And very interesting to help out at the stand telling people about Nim. In this post I want to write out some of the most frequent questions I got while helping out and answer them as a reference for those who might not have seen or been to the stand or didn't have the ability to attend at all but still might be interested in Nim.

Q: What is Nim?

A: Nim is a programming language (note to ourselves: include something on the stand saying "programming language")

Q: A programming language? Why another programming language?

A: Nim was created to be the perfect combination of Python's ease-of-use, C's unsurpassed performance, and Lisp's expressiveness (especially macros). It accomplishes this by being syntactically similar to Python and other scripting languages while remaining statically typed and targeting C as it's main compilation target. It also has a strong macro system which works on the AST level allowing you to create, for example, your own custom Domain Specific Languages (don't worry C programmers, this is nothing like C macros).

Q: So what is Nim intended for?

A: While Nim is described as a systems and applications language it's very general purpose. With the ability to extend it with macros and the ability to compile to both C and JavaScript you can not only run it anywhere, but it will always "fit" your use-case.

Q: Apart from the macros, how does Nim "fit" my use-case?

Another nice feature of Nim is the type system. As mentioned Nim is statically typed, but it also supports distinct types which allows you to leverage the type system to ensure that you're not mixing meters and miles which are both stored as integers. With this you can also overload the common operators meaning dividing "meters" by "seconds" would yield a number having a "speed" type. All this means you can work on the problem abstraction level but get machine-level speed as Nim does all the type checking on compile time while using integers on run-time.

Q: But if it compiles to C/JS surely it will have some overhead over C/JS and will not be as fast.

A: Obviously highly optimized native code will outperform pretty much anything (short of highly optimized assembly). However what you would typically write is not highly optimized native code, the same way human written assembly code is often less optimized than what the compiler outputs. Nim is much the same way, since you don't have to see the C/JS code (but you obviously can) Nim has the ability to create ugly but optimized C/JS code.

Q: Can Nim really run anywhere C can? (partially prompted by the Arduino- and ARM-boards on the stand)

A: Yes! Nim normally runs with a garbage collector, but unlike many other languages the garbage collector can be completely disabled. This means that by doing manual memory management you can run it on even the tiniest of micro-controllers (the smallest we had on the stand was the Digispark with 6Kb of program memory and an Attiny85 chip). Running Nim on such micro-controllers actually has some further benefits you don't get from C, namely the macro and template system. This means you can offload work to compile-time and write easy to understand code which is still high-performance.

Q: So Nim uses a Garbage Collector?

A: Yes, but as mentioned it can be disabled. If you don't want to completely disable it you also have the option of controlling it. So if you have an application where you don't want to have long pauses, like for example a game, you can specify the amount of time to run it for. This is great for real-time systems like the aforementioned games as you can use left-over time between frames to run the GC. In addition Nim comes with multiple GCs so you can choose one that matches your application.

Q: Who is behind Nim? Is there a company backing it?

A: Nim does not have a single commercial entity backing it, it's all open source under the MIT license and community driven. That being said there are companies both using it and contributing to Nim financially.

Q: This all sounds great! Why haven't I heard about Nim before?

A: Well this probably has something to do with the previous question. Nim has been under development for a long time by the original creator, but it's only rather recently that the community has properly kicked off. This means that libraries and support is rapidly being improved.

Q: Speaking of libraries, does Nim support X or Y?

A: While Nim has a lot of "pure" libraries already it's obviously not as rich an ecosystem as for example Python. Since it compiles to C and JavaScript interfacing with existing native libraries is a breeze and there are already a lot of abstraction libraries around common C and JavaScript libraries.

There were of course many more questions, but this is a combined and refined version of the most common answers I got. I want to thank FOSDEM for a great event and I want to thank all the nice people who stopped by our stand and talked to us, hope to see you around in the community! If you want to join said community or come ask your own questions you can find all the community channels over on https://nim-lang.org/community.html.