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

Compile-time Call Count in Nim

The following code does not compile but illustrates what I would like to do: totalTests should hold the number of time that assertEquals() is called (assertEquals() should probably be a macro for this to be possible, but I'm not familiar with this…
x2f
  • 175
  • 10
5
votes
1 answer

defining parameters of multiple types in Nim

Say I have a two classes and a procedure that modifies either class in the same manner. How do I specify that a parameter can be either class (instead of rewriting or overloading the function for each class)? A simple example: type Class1[T] =…
COM
  • 847
  • 9
  • 23
5
votes
3 answers

String interpolation

In scala, you easily include the content of a variable inside a string, like this: val nm = "Arrr" println(s"my name is , $nm") Is this possible in nim, and in that case, how?
Arrrrrrr
  • 802
  • 6
  • 13
5
votes
2 answers

Unit Testing in Nim: Is it possible to get the name of a source file at compile time?

I'm planning a project with multiple modules, and I was looking for a nice solution to run all existing unit tests in the project at once. I came up with the following idea: I can run nim --define:testing main.nim and use the following template as a…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
5
votes
1 answer

Type classes in Nim

I am trying to make a simple use of typeclasses in Nim. Please, keep in mind that I only have been using Nim since this morning, so I may have been doing something stupid. Anyway, I would like to define a pseudorandom generator that produces a…
Andrea
  • 20,253
  • 23
  • 114
  • 183
4
votes
0 answers

Why debugging Nim with GDB yields "No symbol XXX in current context" when trying to introspect variables?

Considering the two different programs below, test.nim and sdl_test.nim, debugging in VSCode using the NativeDebug extension with nim c --out:sdl_test -d:debug --debugger:native sdl_test.nim. When debugging the test.nim program, I can normally…
4
votes
2 answers

Nim sequence assignment value semantics

I was under the impression that sequences and strings always get deeply copied on assignment. Today I got burned when interfacing with a C library to which I pass unsafeAddr of a Nim sequence. The C library writes into the memory area starting at…
Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
4
votes
0 answers

Can't get Pretty Printers to work for debugging Nim code in Codium (VSCode) with GDB (nim-gdb.py)

I tried to debug my Nim code in VSCodium with Native Debug extension and GDB. But I can't get the Pretty Printers to work properly. Here is my launch.json { "version": "0.2.0", "configurations": [ { "name": "Debug x64…
flo
  • 41
  • 2
4
votes
2 answers

How to use the zig compiler in order to compile nim code?

Nim turns its own code into C code and compiles that using C-compilers. Zig has its own compiler that has many nice features that would make you want to use it, such as allowing you to choose which glibc version to dynamically link against, or…
Philipp Doerner
  • 1,090
  • 7
  • 24
4
votes
1 answer

How to generate C headers from Nim

I wrote a library which is intended to be used in C as well. Compiling the library to a .dll/.so/.lib itself is well documented and works as expected. However, I cannot find anything regarding the corresponding header files. The only documentation…
PreFiXAUT
  • 140
  • 9
4
votes
1 answer

Can I unpack `varargs` to use as individual procedure call arguments with nim?

I need to unpack the varargs from a procedure's input to use as a procedure's individual arguments.. In python you can "unpack" lists of arguments for function calls like so: def fun(a, b, c, d): print(a, b, c, d) my_list = [1, 2, 3, 4] #…
RattleyCooper
  • 4,997
  • 5
  • 27
  • 43
4
votes
1 answer

What is a simple and safe way to choose a random enum value?

I needed to choose a random value from an enum. In some article about Nim I found this solution: import random type Animal = enum Cat Dog Cow echo rand(0..2).Animal But this doesn't scale well: If some values are added to or removed from…
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
4
votes
1 answer

String Enum Type in Nim?

Is it possible to define enum-like type for string? I know about enums but don't want to use enums in this case, I want to use it as if it's just a string. type Blog = object text: string priority: "low" | "normal" | "high" echo Blog(text:…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
4
votes
2 answers

How can I use expression inside formatted string in Nim?

The if expression is not working if used inside of fmt string. Why, and how to make it work? import strformat let v = if true: 1 else: 2 # <= Works echo fmt"{v}" echo fmt"{if true: 1 else: 2}" # <= Error
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
4
votes
1 answer

How to silence Nim compile and run output?

If I run nim c -r test.nim the following code in Nim echo "Hi" It would print the result with additional information $ nim c -r test.nim Hint: used config file '/usr/local/Cellar/nim/1.2.6/nim/config/nim.cfg' [Conf] Hint: system [Processing] Hint:…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133