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

Define switch for key=value

In nim, you can define a symbol via -d:value and test if it was defined with defined(value). It is possible however to define a key and retrieve its value? I'm looking for something in the vein of --colors:on but user defined.
Arrrrrrr
  • 802
  • 6
  • 13
3
votes
1 answer

nim re, regex modul is not filling match group

I want to use the regex module of the nim library: import re var s="""
enthus1ast
  • 2,099
  • 15
  • 22
3
votes
2 answers

Nim compiler fails with "Requested command not found: gcc.exe"

I tried to launch first program in nim. I downloaded Windows installer from the official site and did the installation with all possible components. But when I try to run the program I get the message: "unhandled exception: Requested command not…
xtfkpi
  • 151
  • 4
3
votes
1 answer

Does seq assignment create a new seq copy?

Given tow seqs, a and b, declared like this: var a = @[1, 2, 3] b = @[4, 5, 6] will a = b create a new seq copying everything from b to a or, reuse a? I have problems specially regarding to shallowCopy. I cannot tell what are they doing…
Arrrrrrr
  • 802
  • 6
  • 13
3
votes
1 answer

How to get string representation of an expr in Nim template

Is there a possibility to get string representation of an expression (or an identifier) inside a template? For example, having the next code: template `*`*(name: expr) {.immediate.} = var `name`* {.inject.}: string = "" # Want to print 'name'…
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
3
votes
2 answers

Nim macro parameters

here' a code I want to compile: macro defineSomething(amount:expr):stmt= var amountInt = intVal(amount).int # Boring staff defineSomething(42); It works perfectly. I have all I want inside my macro I can operate staff in my own way. But…
Vasiliy Stavenko
  • 1,174
  • 1
  • 12
  • 29
3
votes
1 answer

Nim: Meaning of exclamation mark before a string constant

Reading the macros documentation (http://nim-lang.org/docs/macros.html), I came across this piece of code: nnkIdent(!"echo") I tried to find what the meaning of the exclamation mark is, but could not find anything in the Nim documentation. So: what…
daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
3
votes
1 answer

How to compare two typedesc in a template for equality

I'd like to be able to compare two typedesc in a template to see if they're referencing the same type (or at least have the same type name) but am not sure how. The == operator doesn't allow this. type Foo = object Bar = object template test(a,…
Lye Fish
  • 2,538
  • 15
  • 25
3
votes
1 answer

nim two key table with generics

Trying to create a two key dictionary in Nim where where the value is a user specified type. import Tables type TwoKeyTable[T] = Table[string, Table[string, T]] # initialize two key table proc initTwoKeyTable[T](): TwoKeyTable[T] = result =…
COM
  • 847
  • 9
  • 23
3
votes
1 answer

toSeq(some_string) Type Mismatch

I would like to convert a string to a seq[char] so I can utilize a few of the procs in sequtils, but I'm having an issue with the toSeq template. For example: proc someproc(a, b: string): seq[tuple[a, b: char]] = let s1 = toSeq(a) …
Matt Smith
  • 425
  • 4
  • 9
3
votes
1 answer

Generic Sequences

I have the following snippet. The second variable declaration does not compile though: type Coin = ref object Pen = ref object let yes : seq[ref object] = @[Coin(), Coin(), Coin()] #Compiles no : seq[ref object] = @[Coin(), Pen(),…
Arrrrrrr
  • 802
  • 6
  • 13
3
votes
1 answer

Nim(rod) not installing to path properly?

I'm following the isntructions for installing Nim(rod) onto linux. I then followed this site which showed to do this: $ echo 'export PATH=$PATH:$your_install_dir/bin' >> ~/.profile $ source ~/.profile $ nim Typing nim in the terminal doesn't do…
The User
  • 471
  • 1
  • 4
  • 6
3
votes
1 answer

Hashset in Nim-lang

I am trying to use HashSet type of nim-lang but receiving error var list = initSet\[int]() and error is Error: undeclared identifier: 'initSet' I have already imported hashes library
skvsree
  • 487
  • 1
  • 7
  • 19
3
votes
1 answer

Tuples without field names

I noticed I can declare tuples in nimrod without giving names for each filed. For example: type T1 = tuple[string, age: int] type T2 = tuple[char, string, age: int] But this doesn't apply for the last filed type T3 = tuple[string, int] #…
Svetlin Mladenov
  • 4,307
  • 24
  • 31
3
votes
1 answer

How do I recover from a failed import in Nim?

In Nim I can write the following code to import an external module: import myFancyPantsModule ... # And here I'd use the fancyPants proc This works fine as long as I have the module, but for people who might download the code and not have the…
Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78