A relatively new programming language is Nim. You’re actually holding one of the original books on the subject. The language’s core features, such as its syntax and the semantics of procedures, methods, iterators, generics, templates, and more, are all fixed in place even though it is still not entirely finished. Despite being relatively new, Nim has garnered a lot of attention from the programming community due to the distinctive features that it implements and provides its users.
Before learning Nim, you might have questions, such as why you might want to use it. These questions are addressed in this chapter. This chapter compares Nim to other programming languages, outlines some of its practical applications, and discusses some of its advantages and disadvantages.
Highlight, annotate, and bookmark livebook features
Select the text you want to comment on, bookmark, or highlight, then click the relevant icon. Describe Nim.
A general-purpose programming language called Nim was created to be effective, expressive, and beautiful. Because achieving all three of these objectives at once is challenging, Nim’s designers assigned each of them a different priority, with efficiency taking precedence over elegance.
But even though Nim’s design isn’t particularly concerned with elegance, it is still taken into account. As a result, the language maintains its elegance on its own. Efficiency only triumphs over elegance when trade-offs between the two are necessary.
On the surface, Nim appears to have a lot in common with Python. Particularly, Nim’s syntax shares many features with Python’s, such as the propensity to substitute words for symbols for some operators and the use of indentation to define scope. Nim and Python also have many similarities outside of syntax, such as the extremely user-friendly exception tracebacks, but they also have many differences, particularly in terms of the semantics of the language. The type system and execution model, which you’ll learn about in the following sections, are where the major distinctions lie.
A BIT OF THE HISTORY OF NIM
Nim was created by Andreas Rumpf in 2005. The open source community quickly supported the project and made numerous contributions, with many volunteers from all over the world submitting code via pull requests on GitHub. The list of open Nim pull requests is available at https://github.com/nim-lang/Nim/pulls.
PARTICIPATING IN NIM
All of the Nim-written tools, including the compiler, standard library, and related ones, are open source. Everyone is invited to contribute to the project, which is available on GitHub. Making contributions to Nim is a great way to get to know it and aid in its development. For more details, go to https://github.com/nim-lang/Nim#contributing on Nim’s GitHub page.
Use case examples
From the beginning, Nim was intended to be a general-purpose programming language. As a result, it has a variety of features that enable it to be used for almost any software project. This qualifies it as a strong candidate for writing software across a wide range of application domains, from kernels to web applications. I’ll go over several use cases in this section and explain how Nim’s features and programming support work.
Nim may support almost any application domain, but this does not automatically make it the best option in every situation. The language is better suited for some types of applications than others due to specific features. This isn’t to say that Nim can’t be used to create some applications; it just means that some of the best code models for creating specific kinds of applications might not be supported by Nim.
Although Nim is a compiled language, its compilation process is unique. Source code is first converted into C code by the Nim compiler before being compiled. C is a legacy systems programming language with strong support that makes it simpler and more direct to access the machine’s physical hardware. Because of this, Nim is a good choice for systems programming, making it possible to create things like operating systems (OSs), compilers, device drivers, and embedded system software.
Nim is a powerful tool that is well suited for use with Internet of Things (IoT) devices, which are tangible objects with embedded electronics that are connected to the internet. This is largely because of Nim’s simplicity of use and its systems programming capabilities.
A very basic operating system called NimKernel, which is available on GitHub at https://github.com/dom96/nimkernel, is a good example of a project using Nim’s systems programming capabilities.
HOW IS SOURCE CODE COMPILED BY NIM?
In section 1.1.3, I go into great detail about Nim’s unique compilation model and its advantages.
Applications created in Nim run extremely quickly—often times just as quickly as those created in C and more than thirteen times faster than those created in Python. The highest priority is efficiency, and some features make code optimization simple. This complements a soft real-time garbage collector that enables you to specify how long should be spent collecting memory. This feature becomes crucial during the creation of video games because a regular garbage collector could impede frame rate rendering on the screen if it takes too long to collect memory. Additionally, it helps real-time systems that must function under very strict time constraints.
Nim can be combined with other, much slower languages to accelerate some performance-essential parts. For instance, a Ruby application that needs to perform specific CPU-intensive calculations could benefit greatly from being partially written in Nim. These speedups are crucial in industries like high-speed trading and scientific computing.
Nim offers strong support for programmes that carry out I/O operations, like reading files or sending data over a network. For instance, a variety of web frameworks like Jester (https://github.com/dom96/jester) make it simple to write web applications. These applications can be developed quickly thanks to Nim’s powerful, asynchronous I/O support and script-like syntax.
Nim’s effectiveness can be very useful for command-line programmes. Additionally, Nim applications are standalone and don’t need any significant runtime dependencies because they are compiled. This greatly simplifies the process of distributing them. Nimble, a package manager for Nim that enables users to instal Nim libraries and applications, is one such Nim-written application.
These are just a few use cases where Nim works well; by no means is this a complete list.
Now that you have a basic understanding of what Nim is, how it came to be, and some of the applications it excels in, let’s examine some of its features and discuss how it functions.