Полное руководство по языку программирования
Андреас Румпф
Руководство по Nim 2.0
2я редакция, Авгуcт 2023
(c) 2023 Андреас Румпф
Эта книга посвящается Анжлелике, любви всей моей жизни.
Parts of the book are based on Nim’s manual [https://nim-lang.org/docs/ manual.html] which is the work of many people over many years. I would like to thank everybody who made valuable contributions, such as proof-reading, catching spelling mistakes or pointing out mistakes or omissions in the documentation: Ivan Bobev, Juan Carlos, Federico Ceratto, Neelesh Chandola, Timothee Cour, Huy Doan, Ico Doornekamp, Andrea Ferretti, Jasper Jenkins, James Johnson, Zahary Karadjov, Avahe Kellenberger, Lorenz Krakau, Andrey Makarov, Chris McIntyre, Kaushal Modi, Peter Munch-Ellingsen, Oscar Nihlgård, Rory O’Kane, Dominik Picheta, Andrey Riabushenko, Jacek Sieka, Mathias Stearn, Deansher Thompson, Miran Tuhtan, Elliot Waite, Brian Wignall, Zeshen Xing, Danil Yarantsev, Yanis Zafirópulos.
Special thanks go to Andre von Houck and Miran Tuhtan for reviewing this book. This book would not be as good as it is without your help.
I apologize to anybody that I have left out by accident.
Compiled languages are on the rise again. It’s clear why, they generally produce programs that have better performance over interpreted languages and those programs are of higher quality due to static type checking. Nim goes a step beyond this new wave of compiled languages, by also giving you a powerful macro system to express complex problems. Nim does not depend on any virtual machine, instead producing a binary that runs directly on the CPU — either with the help of an operating system or even without one. Every drop of performance can be squeezed out of your code should the need arise.
Nim’s easy to use static type system catches mistakes early. The type system helps you ensure that a small typo will not break production and a big refactor is no longer scary. You don’t even type that much, type inference is everywhere and generics make it very comfortable to use. Unlike in dynamic languages, every type is known at compile time and is properly optimized for both run time speed and memory space. Meta programming and macros are really powerful and set Nim apart from other static compiled languages. Nim allows you to design deep and composable domain-specific languages with its powerful templates and macros.
Nim offers a “pluggable” memory management system called ARC giving you the required control for low latency, hard real-time systems without having to write “low level” programs. It is good for game and embedded development. ARC also integrates well with custom memory management. Nim is a cross-platform language not tied to any operating system. It feels right at home on Linux, Mac and Windows, and can also be used to make mobile iOS and Android apps. Even on the web it can be used either as
compiled to JavaScript or through WASM.
For embedded programming, the size of the produced machine code is important — Nim also shines in this aspect. The Nim compiler prunes unused code thanks to its focus on static binding needing no runtime introspection features. Where required, Nim’s macro system can be used to provide the required introspection capabilities.
Productivity is also improved thanks to a well designed standard library and a large number of third party packages. The standard library covers topics such as hash tables, common algorithms like sorting or computing the edit distance, powerful collections, wrappers around operating systems, string parsing, unicode and time handling, implementations for the most common internet protocols, math and cryptography algorithms and much more. Software becomes ever more complex and a modern programming language needs to reflect that to some extent. It cannot be “simple” anymore. Many features are taken for granted these days, and rightly so. They enable the construction of complex software much like sophisticated materials and tools enable the construction of skyscrapers. It follows that learning a language is a huge time investment. Nim rewards you with a single coherent language that can be used for everything and it works well on everything: It runs on web browsers, on virtually every operating system as well as on tiny embedded devices and even on GPUs. Nim’s complexity is still very manageable, this book tries to cover Nim completely in about 300 pages. Some describe Nim as a “better Python with types, macros and C’s speed”. The code you write is easy to understand by feeling like running pseudocode, thanks to its indentation-based syntax, short type names, and type inference. It makes people fall in love with programming again by presenting fast compile times, fast run times, minimal boilerplate and a comfortable language. But now enough of the praise, please dive in and be the judge! History and theory behind Nim Nim is a general-purpose language most inspired by Ada, Modula-3, C++, Python and Lisp. Its most important features are type and resource safety, meta programming and combining readability with syntactic convenience. While Nim’s primary focus is “imperative programming”, it does support:
This is the 2nd edition of “Mastering Nim”. It describes version 2.0 of the Nim programming language, first released in 2023-08-01. Who is this for
This book for people who can already program but wish to be able to program in Nim.
This book aims to give the reader a deep understanding of how Nim is layered and structured. It covers how the standard library APIs work and how they were implemented.
This book is structured as follows: Part I is an introduction to Nim via simple algorithms based on graphical programming.
Part II is the largest part, it covers Nim in detail and in a formal language. It tries to convey how Nim really works and ideally you know the language inside out after reading it. This part originally evolved from Nim’s official manual but please do not think that you know it all already! It contains plenty of unique and novel content. Sometimes examples show how the features are supposed to be used or why they exist, sometimes personal advice is provided of what to watch out for or to avoid.
15
Part III focuses on Nim’s macro system. Examples are explained in detail and the examples were chosen to be representative of tasks where macros are an effective tool to improve the readability and the level of abstraction of your code.
Part IV covers Nim’s multi-threading capabilities and memory model. Essential parts of Nim’s standard library are covered in the form of “cheat sheets”. This might seem rather superfluous in the age of the Internet but the online documentation does not provide the information in this concise form. The parts are loosely coupled and can be read independently from each other and in whatever order you choose.