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

printing an array in Nim using echo

Following the example here: https://nim-by-example.github.io/arrays/ and I am printing out an array. In the example they print the matrix, but the echo does not work and I get the following error: matrix.nim(20, 7) Error: type mismatch: got…
disruptive
  • 5,687
  • 15
  • 71
  • 135
4
votes
3 answers

Nim - Create sequence of objects which implement a method

I want to program a game and would like to use a component pattern for multiple entities. In a language with interfaces / type-classes / multiple inheritance there would be no problem. I want some entities to be updateable but not renderable and…
Karroffel
  • 115
  • 6
4
votes
3 answers

How do I write unit test for code that should fail to compile in Nim?

I wrote most of my unit test with the help of the unittest module, but I’m not sure how to use it for code that the compiler should reject at compile time. For example, if I want to write the following code and make sure the compiler always errors…
jxy
  • 784
  • 7
  • 16
4
votes
1 answer

Difference between void proc and void proc with discard

Given the following two procs: proc firstOne(): void = echo "X" proc secondOne(): void = echo "X" discard What functional difference, if any, is there between them? And if they are the same, what is the purpose of discard if void type…
GreenSaguaro
  • 2,968
  • 2
  • 22
  • 41
4
votes
1 answer

Remove element at index from sequence

I have a sequence and I want to remove an element from it at a certain index. How do I do this?
joppiesaus
  • 5,471
  • 3
  • 26
  • 36
4
votes
1 answer

How do I create a directory?

I want to check if a directory exists, if not create it. How can I achieve this? I tried writeFile since I wanted to create a file in it, but that doesn't seem to work.
joppiesaus
  • 5,471
  • 3
  • 26
  • 36
4
votes
2 answers

How to use nimprof?

In one of my Nim projects I'm having performance issues. I'm now trying to use nimprof to see what's going on. I have an import nimprof in my main source file, and I'm compiling with --profiler:on. When I run the program I can see the…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
4
votes
2 answers

Avoiding integer overflow in Nim

I started learning Nim yesterday and decided to code a little test to make a performance comparison with Rust. The code was fairly easy to write and works for values up to 10^9. However, I need to test it with at least 10^12, which gives incorrect…
4
votes
1 answer

Error when i disable gc

When i try to execute nim to run without a garbage collector with --gc:none, i receive the following error message: Error: system module needs 'initStackBottomWith' Since i have found 0 references in the manual and nimc documentation, What does it…
Arrrrrrr
  • 802
  • 6
  • 13
4
votes
1 answer

Nim and SDL2 trouble with Rect

I have the following Nim+official libsdl2 wrapper code import sdl2 discard sdl2.init(INIT_EVERYTHING) let window = createWindow("Tic-Tac-Toe", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 390, SDL_WINDOW_SHOWN) renderer =…
4
votes
1 answer

Cannonical way to do circular dependency in Nim

Suppose we have two modules: one defines an Object and one defines an ObjectFactory. The Object needs to have access to the ObjectFactory use some of its functions, and the ObjectFactory needs access the Object to be able to instantiate…
Vlad Wing
  • 69
  • 10
4
votes
1 answer

Get address from const

From the manual: One can get the address of variables, but one can't use it on variables declared through let statements I understand this is done to provide safety. Now, if i want to get the address from a const at all cost is there a workaround?
Arrrrrrr
  • 802
  • 6
  • 13
4
votes
1 answer

Assigning a function pointer with a forward-declared prototype in Nim

I want to assign a Window Procedure to a Window Class structure: var wndClass : WNDCLASS; wndClass.lpszClassName = CLASSNAME; wndClass.lpfnWndProc = WndProc; wndClass.hInstance = hInstance; I can't assign WndProc yet because it hasn't been…
ilitirit
  • 16,016
  • 18
  • 72
  • 111
4
votes
1 answer

What's the best way to get the current operating system?

I'm looking for something similar to python's sys.platform, which returns 'linux', 'windows', etc, or even better, something like python's platform module which gives you the operating system, distribution, release version, etc.
Ben Doan
  • 53
  • 6
4
votes
2 answers

How do I create a qt app in nim

I'm looking to use nim in an upcoming project, but I'm not sure where to start. I've worked on a fair few webapps over the last two years and I'm pretty good with python. I'm also very good with Linux. Now I want to make a GUI app for linux, maybe…
Ananth
  • 848
  • 11
  • 26