Questions tagged [nim-lang]

Nim (formerly known as "Nimrod") is a statically typed, imperative programming language that tries to give the programmer ultimate power without compromises on runtime efficiency. This means it focuses on compile-time mechanisms in all their various forms.

About Nim

Beneath a nice infix/indentation based syntax with a powerful (AST based, hygienic) macro system lies a semantic model that supports a soft realtime GC on thread local heaps. Asynchronous message passing is used between threads, so no "stop the world" mechanism is necessary. An unsafe shared memory heap is also provided for the increased efficiency that results from that model.

Nim is efficient

  • Native code generation (currently via compilation to C), not dependent on a virtual machine: Nim produces small executables without dependencies for easy redistribution.
  • A fast non-tracing garbage collector that supports soft real-time systems (like games).
  • System programming features:

    • Ability to manage your own memory and access the hardware directly.
    • Pointers to garbage collected memory are distinguished from pointers to manually managed memory.
  • Zero-overhead iterators.
  • Cross-module inlining.
  • Dynamic method binding with inlining and without virtual method table. Compile time evaluation of user-defined functions.
  • Whole program dead code elimination: Only used functions are included in the executable.
  • Value-based datatypes: For instance, objects and arrays can be allocated on the stack.

Nim is expressive

  • The Nim compiler and all of the standard libraries are implemented in Nim.
  • Built-in high level datatypes: strings, sets, sequences, etc.
  • Modern type system with local type inference, tuples, variants, generics, etc.
  • User-defineable operators; code with new operators is often easier to read than code which overloads built-in operators. For example, a =~ operator is defined in the re module.
  • Macros can modify the abstract syntax tree at compile time.

Nim is elegant

  • Macros can use the imperative paradigm to construct parse trees. Nim does not require a different coding style for meta programming.
  • Macros cannot change Nim's syntax because there is no need for it. Nim's syntax is flexible enough.
  • Statements are grouped by indentation but can span multiple lines. Indentation must not contain tabulators so the compiler always sees the code the same way as you do.

Nim plays nice with others

  • The Nim Compiler runs on Windows, Linux, BSD and Mac OS X. Porting to other platforms is easy.
  • The Nim Compiler can also generate C++ or Objective C for easier interfacing.
  • There are lots of bindings: for example, bindings to GTK2, the Windows API, the POSIX API, OpenGL, SDL, Cairo, Python, Lua, TCL, X11, libzip, PCRE, libcurl, mySQL and SQLite are included in the standard distribution or can easily be obtained via the Nimble package manager.
  • A C to Nim conversion utility: New bindings to C libraries are easily generated by c2nim.

Official Resources

IDE

Tutorials

Discussions

652 questions
5
votes
1 answer

Implementing map & min that takes the tables.keys iterator as argument in Nim

I would like to define overloads of map and min/max (as originally defined in sequtils) that works for tables.keys. Specifically, I want to be able to write something like the following: import sequtils, sugar, tables # A mapping from coordinates…
Zecong Hu
  • 2,584
  • 18
  • 33
5
votes
3 answers

What is nim's equivalent of Python's `sys.executable`?

Following is the content of foo.py import sys print(sys.executable) When I execute this, I can get the full path of the the Python interpreter that called this script. $ /mingw64/bin/python3.9.exe foo.py /mingw64/bin/python3.9.exe How to do this…
Dilawar
  • 5,438
  • 9
  • 45
  • 58
5
votes
2 answers

How to evaluate an expression in nim?

I'm trying to perform the equivalent of the eval method from Python in nim. I was under the impression that parseStmt from the macros package should help me with this, but I'm facing a compilation issue that I don't understand. import macros echo…
Benjamin Audren
  • 374
  • 2
  • 16
5
votes
2 answers

How can I get safe reference to existing object in Nim?

I wrote a Nim procedure taking two objects as var parameters. They each are an object with an int field level. Before doing the real work of the procedure, I want to put the parameters in order by which has the larger level, so I do this: proc…
Dave Doty
  • 285
  • 1
  • 9
5
votes
1 answer

How can I get stack trace from exception in Nim?

There's the getStackTrace() function that gets stack trace from the current exception. But it doesn't work for specific exception, this code won't work error.getStackTrace() I need it for log function proc error*(message: string, exception:…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
5
votes
2 answers

Nim return custom tuple containing proc

I'm trying to create a proc that returns a custom tuple that contains a single element that is a proc type i.e. type CustomTuple = tuple foo: proc(input: int): int proc createCustomTuple(): CustomTuple = (foo: proc(input: int): int =…
Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
5
votes
2 answers

How to call a Nim function from Rust through C-FFI?

Nim backend integration guide describes how to call a Nim function from C. Example function: proc fib(a: cint): cint {.exportc.} = if a <= 2: result = 1 else: result = fib(a - 1) + fib(a - 2) The procedure requires that the Nim compiler…
noconst
  • 639
  • 4
  • 15
5
votes
1 answer

How to convert from single character to string in Nim?

In Nim, I have a string that I need to split into characters, but each character should be converted to a string. Right now I have something like: var d = initTable[string,int]() for ch in line: d.mgetOrPut(ch, 0) += 1 This fails, because ch is…
Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139
5
votes
1 answer

How to obtain the nim compiler version programmatically ?

Is there any constant variable or proc that allows to access the compiler version as a string or number?
Jundiaius
  • 6,214
  • 3
  • 30
  • 43
5
votes
3 answers

How to develop on the stack with Nim?

I would like to use Nim in a soft-realtime context, where both memory allocation and garbage collection exhibit too much latency. Therefore, manual memory management is desirable- or, even better, working exclusively from stack memory. Which subset…
cmc
  • 4,294
  • 2
  • 35
  • 34
5
votes
2 answers

How to debug print a variable (name) and its value in Nim?

During quick and dirty debugging, I often use expressions like: echo "variable1: ", variable1, " variable2: ", variable2 How can I leverage Nim's macro system to avoid repeating the variable name?
bluenote10
  • 23,414
  • 14
  • 122
  • 178
5
votes
1 answer

What does "special compiler magic" mean?

In the Nim system module it says: Most of the routines listed here use special compiler magic. What do they mean by "special compiler magic"?
dgo.a
  • 2,634
  • 23
  • 35
5
votes
2 answers

Nim stored procedure reference in tuple

Nim Compiler Version 0.13.0 (2016-01-19) [Windows: i386] How would I store a reference to a procedure in a tuple: Job = tuple[every:int, timescale:string, timestr:string, jobfunc:proc] proc run(job: Job, jobfunc: proc): Job = result = job …
enthus1ast
  • 2,099
  • 15
  • 22
5
votes
1 answer

Nim `Warning: re is deprecated`, what to use instead?

I'm writing a Nim program using regexes, which works fine, except that when I compile, I get this error message: Warning: re is deprecated [Deprecated] I've looked in the documentation for the re module, but there's no mention of a new way to…
squirl
  • 1,636
  • 1
  • 16
  • 30
5
votes
3 answers

reading files from tar.gz archive in Nim

Looking for a way to read in a file from a tar.gz archive using the Nim programming language (version 0.11.2). Say I have an archive /my/path/to/archive.tar.gz and a file in that archive my/path/to/archive/file.txt My goal is to be able to read…
COM
  • 847
  • 9
  • 23