Introduction to Metaprogramming in Nim

Wikipedia gives us a nice description of metaprogramming:

Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse and/or transform other programs, and even modify itself while running.

In this article we will explore Nim‘s metaprogramming capabilities, which are quite powerful and yet still easy to use. After all great metaprogramming is one of Nim’s main features. The general rule is to use the least powerful construct that is still powerful enough to solve a problem, in this order:

  1. Normal procs and inline iterators
  2. Generic procs and closure iterators
  3. Templates
  4. Macros

So before looking at Nim’s two main metaprogramming constructs, templates and macros, we’ll look at what we can do with procs and iterators as well.


Nim for scientific computing

“In the last few months, I have been shifting the focus of my work towards scientific computing, be it for cryptographic applications, machine learning or neural networks. I have been hard-pressed to find an environment that satisfies me fully.

I do most of my daily work in Scala, and while I am still a big fan of it, trying to make it into a tool for scientific programming often hits its limits. A few things are desirable:

  • raw speed, in particular when writing inner loops
  • a predictable memory layout is necessary to be cache-friendly and to interface easily with C libraries
  • the possibility to interface easily with dedicated hardware such as GPUs and FPGAs
  • did I mention speed?…”

Nim binary size from 160 KB to 150 Bytes

“The size of binaries in the Nim programming language seems to be a popular topic recently. Nim’s slogan is expressive, efficient, elegant, so let’s examine the efficient part in this post by exploring a few ways to reduce the size of a simple Nim Hello World binary on Linux. Along the way we will:

  • Build a regular program into a 6 KB binary
  • Disregard the C standard library and build a 952 byte binary
  • Use a custom linker script and ELF header to build a 150 byte binary
    (1 byte smaller than in Rust)

The full source code of this post can be found in its repository. All measurements are done on Linux x86-64 with GCC 5.1 and Clang 3.6.0…”

Nim: How I Start

Nim is a young and exciting imperative programming language that is nearing its 1.0 release. My main motivation for using Nim is its performance / productivity ratio and the joy of programming in Nim. In this guide I’m going to show you how I start a Nim project.

For this purpose we will write a small interpreter for the brainfuck language. While Nim is a practical language with many interesting features, brainfuck is the opposite: It’s impractical to write in and its features consist of 8 single-character commands. Still, brainfuck is great for us, since its extreme simplicity makes it easy to write an interpreter for it. Later we will even write a high-performance compiler that transforms brainfuck programs into Nim at compile time. We will put all of this into a nimble package and publish it online…”

A Quick Comparison of Nim vs. Rust

Rust and Nim are the two new programming languages I have been following for a while. Shortly after my previous blog post about Rust, Nim 0.10.2 was out. This led me to take a closer look at Nim, and, naturally, compare it with Rust.

In this blog post, I’m going to share my two simple examples written in Nim and Rust, the rough execution time comparison, and my subjective impressions of coding in both…”