Nim programming language version 2.0 released, compiles to native code or JavaScript

Nim programming language version 2.0 released, compiles to native code or JavaScript
Coding

The Nim team has released version 2.0 of the Nim programming language, described as evolutionary, now with ORC (cyclic reference counting) as the default for memory management.

Nim is a general purpose language that compiles to both native code for Windows, Linux or Mac OS, and to JavaScript. Nim has several options for memory management, with the previous default being ARC (automatic reference counting). A more advanced memory management system called ORC, first released with Nim 1.4 in December 2020, is now complete and the default in Nim 2.0. ORC is ARC plus a cycle collector.

When ORC was introduced, Nim designer Andreas Rumpf compared ORC to a mark and sweep garbage collector and said that: “ORC always wins on latency and memory consumption; plays nice with destructors, and hence with custom memory management; is independent of the heap sizes; tracks stack roots precisely and works cleanly with all sanitizers the C/C++ ecosystem offers.”

Other features in Nim 2.0 include improved type inference, new standard library modules with better type safety when dealing with paths, files and directories, overloadable enums (previously experimental), and default values for object fields. The JavaScript compiler now using BigInt for 64-bit integer types by default. C++ interop is improved.

A new experimental switch called strictDefs enforces that a variable has been assigned a value before it can be used. “We found Nim’s default initialization rule to be one major source of bugs,” said the Nim team, introducing version 2.0.

Using Nim in Visual Studio Code

In the Nim forum Rumpf said that his favourite new feature was overloadable enums. He also apologized for not yet completing incremental compilation, which has the potential to improve compile time. “Bugs aside, the way we implemented IC doesn’t show enough of a performance benefit so we’ll redo the way it works yet again. Sorry,” he said.

Nim is a free and open source project using the MIT license. It is a niche language, used by just 0.38% of developers according to the latest Stack Overflow survey, but it is loved by its users thanks to the combination of fast native code, memory safety, easy interop with C and C++, and a compile to JavaScript option that enables use in the browser. It is suitable for system and embedded programming. Memory safety is a critical feature, since memory issues account for many bugs and vulnerabilities in languages such as C and C++. According to the FAQ, “Nim provides memory safety by not performing pointer arithmetic, with optional checks, traced and untraced references and optional non-nullable types.”

Another advantage of Nim is that it relatively easy to learn. The language “borrows heavily from (in order of impact): Modula 3, Delphi, Ada, C++, Python, Lisp, Oberon,” says the FAQ.

A developer on Hacker News, who uses Nim in production, said: “It’s been a real joy to work with and reminds me of when I discovered D back in the day, only it’s even better. If you imagine native-compiled type-annotated Python where nearly 100% of your code is business logic with no cruft, you’re getting close to the Nim experience.”