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

interop with nim return Struct Array containing a string /char* member

interoping nim dll from c# i could call and execute the code below if i will add another function (proc) that Calls GetPacks() and try to echo on each element's buffer i could see the output in the C# console correctly but i could not transfer the…
Avia Afer
  • 866
  • 2
  • 8
  • 29
11
votes
2 answers

How to create a nim dll and call it from c#

I have read almost every example I could find via google, and couldn't accomplish the simplest task creating a dll (windows) from nim Could anyone explain it step by step? I am using the nim IDE - aporia to produce the code. Does building a dll…
Avia Afer
  • 866
  • 2
  • 8
  • 29
11
votes
1 answer

Nim: How to prove not nil?

To me, one of the most interesting features of Nim is the not nil annotation, because it basically allows to completely rule out all sorts of NPE / access violations bugs statically, by the help of the compiler. However, I have trouble to use it in…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
10
votes
2 answers

How to write to stderr in Nim?

I know there is echo, which writes to stdout. Is it possible to redirect echo to stderr, or is there another way to write to stderr?
bluenote10
  • 23,414
  • 14
  • 122
  • 178
10
votes
1 answer

Nim how to define constructors?

Is there a way in Nim to define constructors for an object? For example I have type Deck* = ref object cards* : array[52, Card] can I create an empty constructor that creates automatically all the cards?
Mikedev
  • 316
  • 1
  • 8
10
votes
2 answers

How to sort a sequence in Nim?

I have a sequence generated by list comprehension as follows: var a_bigram_list = lc[a[i..i+2] | (i <- 0..
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
10
votes
3 answers

Nim: Addresses of parameters and mutability

I'm trying to make up my mind about Nim's policy behind expression has no address. In particular, I have a C function which takes a pointer (+ length etc.) of some data buffer. I know that this function will not modify the data. Simplified: type …
bluenote10
  • 23,414
  • 14
  • 122
  • 178
10
votes
3 answers

Where does Nim search for modules to import?

When using the import statement, how/where does Nim perform its search for modules? I know that file paths can be used, but if I don't want to use a file path, where would I put the module I defined locally on my machine? I haven't used Nimble yet…
Lye Fish
  • 2,538
  • 15
  • 25
10
votes
1 answer

Nim inter-thread message passing: How to avoid a global TChannel?

I have the following simple example of an inter-thread communication problem: I want to run arbitrary "anytime" algorithms in a background thread. An anytime algorithm performs some computation of result type T incrementally, i.e., it sporadically…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
10
votes
1 answer

Nim: How to iterate over a slice?

I'm puzzled by the following observation. On the one hand, this works: for i in 5..10: echo i But as soon as I store the slice in a variable, I can no longer iterate over it, i.e., this fails: var slice = 5..10 for i in slice: echo i The error…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
10
votes
2 answers

Ada-like types in Nimrod

today I did ask in D mailing list whether it's possible to define and use custom data types in a way similar to e.g. example from Ada's wiki page: type Day_type is range 1 .. 31; type Month_type is range 1 .. 12; type Year_type is range…
gour
  • 967
  • 9
  • 22
9
votes
1 answer

How to return an exit code from the main block in Nim?

In Nim, to write code that executes as a sort of main function, you do (similar to the is main checks in Python): when isMainModule: echo ("Hello, Nim!") However, for the life of me, I can't figure out how to return an error code. Traditionally…
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
9
votes
1 answer

Tuple to function arguments in Nim

Can I convert a tuple into a list of function arguments in Nim? In other languages this is known as "splat" or "apply". For example: proc foo(x: int, y: int) = echo("Yes you can!") type: Point = tuple[x, y: int] let p: Point = (1,1) # How to…
Imran
  • 12,950
  • 8
  • 64
  • 79
9
votes
1 answer

typed vs untyped vs expr vs stmt in templates and macros

I've been lately using templates and macros, but i have to say i have barely found information about these important types. This is my superficial understanding: typed/expr is something that must exists previously, but you can use .immediate. to…
Arrrrrrr
  • 802
  • 6
  • 13
9
votes
2 answers

Nim cross compilation to C

I wrote a Nim program, echo("Hello.") And then I tried to cross compile for a Linux machine, nim c --cpu:i386 --os:linux -c hello.nim This produced the following output: config/nim.cfg(45, 2) Hint: added path: '/Users/connor/.babel/pkgs/'…
cjohnson318
  • 3,154
  • 30
  • 33
1
2
3
43 44