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
8
votes
1 answer

calling nim proc from NodeJs

Following the docs here: https://nim-lang.org/docs/backends.html#backends-the-javascript-target I have the following code in fib.nim proc fib(a: cint): cint {.exportc.} = if a <= 2: result = 1 else: result = fib(a - 1) + fib(a - 2) If I…
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
8
votes
1 answer

How do I handle Ctrl-C interrupt in nim?

If I press Ctrl-C while my program is running it exits and prints SIGINT: Interrupted by Ctrl-C. How do I ignore the Ctrl-C interrupt in Nim on Linux? Thanks in advance.
joe
  • 463
  • 4
  • 17
8
votes
1 answer

Concatenation of string and int value in nim

I want to concatenate string and int. But it doesn't work with & and add operator. echo "abc" & 2 But it doesn't work.
frogEye
  • 364
  • 3
  • 14
8
votes
1 answer

Nim: standard way to convert integer/string to enum

The question is Nim language specific. I am looking for a standard to way to convert integer/string into enum in a type safe way. Converting from enum to integer/string is easy using ord() and $(), but i can't find an easily way to make opposite…
Andrey R
  • 175
  • 2
  • 7
8
votes
1 answer

what is nim type definition for generic procedure?

I have a strategies expressed as generics in nim: proc fooStrategy[T](t: T, ...) proc barStrategy[T](t: T, ...) I would like to create a lookup table for the strategies by name... so I tried: type Strategy*[T] = proc[T](t: T, ...) let…
shaunc
  • 5,317
  • 4
  • 43
  • 58
8
votes
1 answer

Statically linking Nim code to Go

I'm trying in Linux to statically link some code created in Nim into a Go application. I've followed the Nim Backend Integration docs and some articles for linking C in Go but haven't gotten it working. Here's where I'm at so far... Nim code…
Lye Fish
  • 2,538
  • 15
  • 25
8
votes
1 answer

Nim code parser

There are parsers available in the macros package, like parseExpr and parseStmt but they're {.compileTime.} procs. Is there any way to parse a string of Nim code at runtime, yielding an AST that can be analyzed?
Lye Fish
  • 2,538
  • 15
  • 25
8
votes
1 answer

What's the meaning of "magic" pragma in nim?

When going through the code in Nim project itself, I find that some proc decorated by "magic" pragma misses proc definition (example). There's no doc to explain the pragma, I guess the proc's definition is somewhere else and is merged while…
Roger
  • 440
  • 2
  • 13
8
votes
1 answer

Clear a sequence in Nim

What is the Nim equivalence of List.Clear in languages like java or c# for sequences? I see listed in system the proc setLen, but im not sure it does what i want. From the description: f the current length is greater than the new length, s will be…
Arrrrrrr
  • 802
  • 6
  • 13
8
votes
2 answers

create a reference to an array in Nim

var b: array[5, int] type ArrRef = ref array[5, int] var c : ArrRef echo repr(c) # nil c = addr b # doesn't compile, says type is Array constructor, expected reference In Nim, how can I pass references to arrays instead of passing by value?…
talloaktrees
  • 3,508
  • 6
  • 28
  • 43
8
votes
2 answers

Nim: How to wrap/derive an iterator from another iterator?

Let's assume we have some existingIterator which iterates over elements of an arbitrary type T. What I now want to achieve is to derive a new iterator from existingIterator with a modified behavior. Think of examples like: Limiting the length of…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
8
votes
1 answer

How to change Nim compiler output file location and name

Compiling a Nim program with nim c -r example.nim creates the output file example. I would like to create an output file in another folder with the name bin/example.o which is much easier to gitignore. What I've tried so far: nim c -r example.nim…
Matt Smith
  • 425
  • 4
  • 9
8
votes
1 answer

How can I use function pointers in Nimrod?

Is it possible to use function pointers in Nimrod? What I've tried is: type fptr = (proc(int):int) proc f(x:int): int = result = x+1 var myf : fptr = f echo myf(0) but when I try to compile I get: Hint: added path:…
Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75
8
votes
2 answers

In Nimrod, what is the syntax for bitwise operations?

I'm just discovering Nimrod and have a basic question (couldn't find the answer in the documentation). How do you use bitwise operations ? I have the following code, where x is defined as an int : if x and 1: This does not compile : Error: type…
Fabien
  • 12,486
  • 9
  • 44
  • 62
8
votes
1 answer

Brainfuck interpreter in Nimrod

I am toying with nim (at the time of writing still called nimrod), by writing a Brainfuck interpreter in the language. Without loops implemented, I have: import os, unsigned const RamSize = 200 type TRam = array[0..RamSize, int] var ram :…
1 2
3
43 44