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
4
votes
2 answers

How to print without skipping a line in Nim

I would like to print without skipping lines in Nim. this is my code by far int i = 1 for i in countup(1, 10): echo "number: " echo i I would like the output to be: number: (i value) ... instead of: number: (i value)
user14074696
4
votes
1 answer

Nim. How to inherit all operations on distinct type?

Suppose, I have type Radians = distinct float and type Degrees = distinct float This is not allowing me to use all operations available for floats, even most basic arithmetics +, -, * Is there any way to sort of 'inherit' them all and use distinct…
IC_
  • 1,624
  • 1
  • 23
  • 57
4
votes
1 answer

translating 2014 Nim to 2019 Nim

I stumbled upon this code defining a DSL for html: template html(matter: stmt) {.dirty.} = var result = "" matter template tag(tagName) = template `tagName`(attrs: varargs[expr], matter: stmt = nil) {.dirty.} = # formatAttrs closes the…
onqtam
  • 4,356
  • 2
  • 28
  • 50
4
votes
2 answers

Is Nim similar to Java in its memory overhead?

In 64-bit Java, each object instance tends to include a 192-bit header, containing the class pointer, flags and locks (64 bits each). This can cause a large memory overhead for small objects. Is the situation similar for Nim? Would a large…
MWB
  • 11,740
  • 6
  • 46
  • 91
4
votes
1 answer

Warning: 'matchIter' is not GC-safe as it accesses 'x' which is a global using GC'ed memory [GcUnsafe2]

When building this code in nim: import jester, asyncdispatch let stuff = "thing" routes: get "/": resp stuff runForever() it results in: mytest.nim(3, 1) template/generic instantiation from here lib/core/macros.nim(369, 70)…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
4
votes
1 answer

How to run a line of code in debug mode only in Nim?

I have a line of code that I want to run in debug mode but not in release mode. Is there a way to automatically handle this?
Imran
  • 12,950
  • 8
  • 64
  • 79
4
votes
1 answer

Passing iterator as argument in Nim fails "attempting to call undeclared routine"

I'm trying to learn Nim by writing a certain bioinformatics tool that I already implemented in other languages. I have the following version that compiles and runs apparently correctly: from strutils import join from sequtils import zip type …
bli
  • 7,549
  • 7
  • 48
  • 94
4
votes
1 answer

Nim: find the index of an item in an array or sequence

Is there a builtin method to find the index of an item in an array or sequence, equivalent to Python index ? (It may return the index of the first occurrence, or all the indices.)
Adrien
  • 469
  • 3
  • 11
4
votes
0 answers

Nim: Parallel loop with mutating state

I'm new to the Nim language. I wanted to learn it by implementing a simple Genetic Algorithm to evolve strings (array of integers atm) by distributing the work to the CPU cores: https://github.com/peheje/nim_genetic I have successfully parallelized…
Peheje
  • 12,542
  • 1
  • 21
  • 30
4
votes
1 answer

How can the "static type" and "dynamic type" be different?

According to the Nim manual, the variable type is a "static type" while the actual value the variable points to in memory is the "dynamic type". How is it possible they can be different types? I thought assigning the wrong type to a variable would…
dgo.a
  • 2,634
  • 23
  • 35
4
votes
1 answer

Asynchronous code (javascript's setTimeout equivalent) in Nim

What would be a nim equivalent of this javascript program with setTimeout? Please don't hack with sleep(1000) and keep the code asynchronous. setTimeout( function() { console.log("Hello world") }, 1000 )
user619271
  • 4,766
  • 5
  • 30
  • 35
4
votes
2 answers

How to properly pass arguments via command line in NIM?

I am using the following snippet to parse command line arguments and store them in a table. var args = initTable[string, string]() for kind, key, val in getopt(): args.add(key,val) However, it works only if I pass = in the command line ./mytool…
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
4
votes
1 answer

Converting an int type to a uint8

The Nim Tutorial page states: Lossless Automatic type conversion is performed in expressions where different kinds of integer types are used. So, I thought that creating an int in the range of a uint8 would allow me to pass it to procs expecingt…
GeckStar
  • 1,136
  • 1
  • 14
  • 22
4
votes
1 answer

What is the best way to bundle static resources in Nim?

Currently, I am writing a web application using Jester and would like to facilitate the deployment by bundling all static resources (CSS, HTML, JS). What is the best way to do this in Nim ?
Pythtotic
  • 415
  • 6
  • 10
4
votes
2 answers

Nim: How to check if a float is nan or inf?

How can I check if a float variable is NaN or inf. Searching the standard library for isNaN or isInf does not reveal anything. There is a blog post suggesting to use proc cIsNaN(x: float): int {.importc: "isnan", header: "".} but is there…
bluenote10
  • 23,414
  • 14
  • 122
  • 178