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

What is the model of value vs. reference in Nim?

NOTE: I am not asking about difference between pointer and reference, and for this question it is completely irrelevant. One thing I couldn't find explicitly stated -- what model does Nim use? Like C++ -- where you have values and with new you…
greenoldman
  • 16,895
  • 26
  • 119
  • 185
25
votes
2 answers

How to get the type of a value as string?

I would like to know if it is possible to get the type (int32 / float64 / string) from a value in Nim at runtime? I thought this would be possible with the "typeinfo" library but I can't figure it out! EDIT: Got an answer and made this real…
OderWat
  • 5,379
  • 3
  • 29
  • 31
23
votes
5 answers

How can I create a new primitive type using C++11 style strong typedefs?

I'm trying to emulate in C++ a distinct type from the Nim programming language. The following example won't compile in Nim because the compiler catches the variables e and d having different types (Error: type mismatch: got (Euros, float))…
Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78
22
votes
3 answers

Safely "lend" memory block to another thread in C, assuming no "concurrent access"

The problem I want to allocate memory in one thread, and safely "lend" the pointer to another thread so it can read that memory. I'm using a high level language that translates to C. The high level language has threads (of unspecified threading API,…
Sebastien Diot
  • 7,183
  • 6
  • 43
  • 85
22
votes
4 answers

Nim equivalent of Python's list comprehension

Since Nim shares a lot of features with Python, i would not be surprised if it implements Python's list comprehension too: string = "Hello 12345 World" numbers = [x for x in string if x.isdigit()] # ['1', '2', '3', '4', '5'] Is this actually…
Arrrrrrr
  • 802
  • 6
  • 13
20
votes
4 answers

How to get access to command-line arguments in Nim?

How can I access command line arguments in Nim? The documentation shows only how to run the compiled Nim code with command line arguments nim compile --run greetings.nim arg1 arg2 but I didn't find how to use their values in code.
Lucian Bredean
  • 803
  • 1
  • 10
  • 21
20
votes
1 answer

Common patterns to work around the limitations of thread-local gc?

In my process of learning Nim, I'm currently studying Nim's approaches to concurrent programming. I have seen a few comments about the limitations of a thread-local garbage collection (for instance here and there), but I still don't fully see all…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
19
votes
3 answers

How do I get the c code of a nim program?

I followed the docs and compiled with nim compileToC helloworld.nim but it just spit out an executable. How can I see the intermediate C representation?
Elliot Gorokhovsky
  • 3,610
  • 2
  • 31
  • 56
17
votes
1 answer

writing/reading binary file in Nim

What's the best way to write and read a binary files in Nim? I want to write alternating floats and ints to a binary file and then be able to read the file. To write this binary file in Python I would do something like import struct # list of…
COM
  • 847
  • 9
  • 23
16
votes
1 answer

Is there are something like Python's 'pass' statement in Nim

I am a newer to Nim programing language. when I use Python, I can use the 'pass' to skip the defination detail of a function and class. def foo(): pass # skip detail class Bar(): pass Is there are something like this in Nim?
PKT233
  • 163
  • 6
16
votes
3 answers

Converting a seq[char] to string

I'm in a situation where I have a seq[char], like so: import sequtils var s: seq[char] = toSeq("abc".items) What's the best way to convert s back into a string (i.e. "abc")? Stringifying with $ seems to give "@[a, b, c]", which is not what I want.
Sp3000
  • 374
  • 2
  • 10
15
votes
2 answers

Modulus operator nim

What is the modulus operator in Nim? tile % 9 == 0 results in undeclared identifier: '%' Googling or searching SO doesn't bring up an answer.
Lex
  • 4,749
  • 3
  • 45
  • 66
14
votes
4 answers

How to convert object to json in Nim

I'm making a small web service in Nim, and I need to respond to requests with json. I'm using the jester module to make the service. I expect I can use the json module in Nim's base library to construct some kind of object with fields and values,…
Torbjørn
  • 6,423
  • 5
  • 29
  • 42
12
votes
1 answer

What exactly are strings in Nim?

From what I understand, strings in Nim are basically a mutable sequence of bytes and that they are copied on assignment. Given that, I assumed that sizeof would tell me (like len) the number of bytes, but instead it always gives 8 on my 64-bit…
Lye Fish
  • 2,538
  • 15
  • 25
11
votes
2 answers

How to iterate over a tuple in Nim?

Let's say I have a procedure getTuple(): (int, int, int). How do I iterate over the returned tuple? It doesn't look like items is defined for tuple, so I can't do for i in getTuple(). I initially had this returning a sequence, which proved to be a…
Imran
  • 12,950
  • 8
  • 64
  • 79
1
2 3
43 44