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

Format (a float to string with no decimals) in nim

I have a float, say 14.55e9 and I want to stringify it to "14550000000" the most nimic, clean and performant way as possible. For now I only could find something based on slicing: $myFloat[0..$myFloat.find('.')] The formatFloat method from strutils…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
0
votes
1 answer

Inline sorting of complex type sequence

I have the following problem: I have a table mapping point IDs (uint16) to Points (tuple[x: float64, y: float64]). Given three point IDs, I want to create a Triangle which is a triple of point IDs sorted by their x-values (and secondarily sorted by…
GeckStar
  • 1,136
  • 1
  • 14
  • 22
0
votes
1 answer

conflicting types when compiling Nim and SDL2 Image with Emscripten

I try to compile a Nim project that uses SDL2 Image with Emscripten, but I get several conflicting types errors. I could reproduce this issue in this small code snippet: index.nim import sdl2, sdl2.image const imgFlags: cint = IMG_INIT_PNG if…
maiermic
  • 4,764
  • 6
  • 38
  • 77
0
votes
0 answers

Error when compiling nim file to generate javascript Code

I have tried to compile nim file to generate javascript code but I got this error: Error: OS module not ported to your operating system! using this command: nim js -d:nodejs -r myFile.nim. The problem is related to : import os in this file. Is there…
EFREV123
  • 33
  • 4
0
votes
1 answer

Nim FFI: Using existing type to wrap a C++ type

I'm putting together a Nim wrapper for Box2D using c2nim. Box2D has its own mathematical vector class, b2Vec2. If I were using Box2D in raw C++, I'd just use that class. Simple. But Nim already has such functionality in the basic2d module's…
JesseTG
  • 2,025
  • 1
  • 24
  • 48
0
votes
1 answer

Nim: On mac, cannot open standard import

I'm using nim on Mac, and am having trouble with this line: from strutils import parseInt It gives this error when trying to compile: csvx.nim(1, 6) Error: cannot open 'strutils' ~/dev/polyglot/csvx/ I installed nim using the "Installation based on…
Christopher Davies
  • 4,461
  • 2
  • 34
  • 33
0
votes
1 answer

Nim: how can I make it closer to Python syntax?

I know it's generally NOT a good idea but I want to make Nim more "Pythonic". Examples: 1) instead of proc, use def 2) instead of echo, use print 3) instead of readLine, use input 4) instead of parseJson use json.loads and so on. Yes, it may not be…
Spaceman
  • 1,185
  • 4
  • 17
  • 31
0
votes
3 answers

Passing complex parameters to a Nimrod macro

I would like to pass configuration parameters to a macro. I already have a procedure that produce a string of Nimrod code based on these parameters (a tuple of sequences). I know that I can pass a string and convert it with strVal (as shown the…
Clement J.
  • 3,012
  • 26
  • 29
-1
votes
1 answer

Device handling in Nim

I am looking for a library that could capture streams of images from webcam or USB camera, and then converting image data into multidimensional matrices, in order to do some mathematical operation on them; afterward saving the result as a png…
Shoaib Mirzaei
  • 512
  • 4
  • 11
-1
votes
1 answer

Prologue -Webframework - How to set thread-local variables for logging when compiling with `--threads:on` flag?

I am writing a web-application in nim, using the web-framework prologue and the ORM norm. I've found that the log-messages of prologue and norm that normally appear in the terminal when starting up the application disappear, when you compile with…
Philipp Doerner
  • 1,090
  • 7
  • 24
-1
votes
1 answer

Nim - How to access raw POST request body in prologue framework

I recently picked up Nim and am in the process of re-implementing an existing web-application of mine to get some experience in the language. This web-application used JWT for authentication, with the typical split into an access-token and a…
Philipp Doerner
  • 1,090
  • 7
  • 24
-1
votes
1 answer

Nim-lang (nimrod) fatal error: limits.h: No such file or directory

I'm learning Nim language. Whenever I try to run the code using this command: nim c --run hello.nim, I get this error: Hint: used config file '/etc/nim/nim.cfg' [Conf] Hint: system [Processing] Hint: widestrs [Processing] Hint: io [Processing] Hint:…
user9453520
-1
votes
2 answers

isNil can't be used on literals

echo 0.isNil type mismatch: got < int literal(0)> but expected one of: proc isNil[T: proc](x: T): bool proc isNil[T](x: ptr T): bool proc isNil(x: cstring): bool proc isNil(x: pointer): bool proc isNil(x: string): bool proc…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
-1
votes
2 answers

What is Nim's approach to distinguish between commands?

I'm trying to understand what kind of approach is used by Nim to distinguish between commands. There's the "separatist approach" where a semicolon just separates commands (used in Pascal for example), the "terminist approach" where a semicolon…
BeH
  • 9
  • 4
-1
votes
1 answer

Using templates to construct import

Within a file i have several imports from the same directory. If i change the location of this file, rather than having to add one to one '../' inside import, i'd like to use a template to build them and make my life easier when it comes to change…
Arrrrrrr
  • 802
  • 6
  • 13
1 2 3
43
44