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

nim - custom macro/pragma to get ast of complete module but get "cannot attach a custom pragma"

I want to get access to the AST of a complete module (file) with nim. I found, that any macro can be used as a custom pragma, so I did something like this in file foo.nim: import macros macro getAst(ast: untyped): untyped = echo "ast = ",…
Ridcully
  • 23,362
  • 7
  • 71
  • 86
3
votes
1 answer

nim: Use a static library

I've tried to get an audio library statically linked to my program. I use this nimble package. To get it run, i had to build the soloud library as described here. For short after download i ran "genie --with-miniaudio-only --platform=x64 vs2017" in…
Aquachain
  • 31
  • 4
3
votes
3 answers

how to iterate with two iterators

I would like to use nimbioseq and iterate two files with the same number of sequences (using the readSeq()iterator), as: for seq1, seq2 in readSeq(file1), readSeq(file2): echo seq1.id, "\t", seq2.id For this scenario I suppose I need some sort of…
Andrea T.
  • 920
  • 4
  • 15
3
votes
1 answer

How to get Captured Part of the Regular Expression in Nim

I want to extract "some_token" from the "some text :some_token" text. The code below returns the full match ' :some_token' not the captured part 'some_token' marked with ([a-z0-9_-]+). import re let expr = re("\\s:([a-z0-9_-]+)$", flags =…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
3
votes
2 answers

How to divide int64 in Nim?

How can I divide int64? let v: int64 = 100 echo v / 10 Error Error: type mismatch: got Full example import math proc sec_to_min*(sec: int64): int = let min = sec / 60 # <= error min.round.to_int echo…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
3
votes
2 answers

Iterator-producing function in Nim: works when assigning the iterator, stuck when calling it directly

I tried to make a procedure that creates an iterator, as follows: proc makeCDFrom(start: int): iterator(): int = result = iterator(): int = var i: int = start while i >= 0: echo "i:", i yield(i) dec(i) let cdFrom6…
bli
  • 7,549
  • 7
  • 48
  • 94
3
votes
1 answer

How to declare nullable field in Nim?

The stock_symbol is optional, it exists only for some companies, what would be proper way to declare it in Nim? Do I have to use ref or there's other way? type Company = object name: string stock_symbol: string echo Company(name:…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
3
votes
1 answer

How to convert sequence of objects to JSON in Nim?

I'm trying to convert list of objects to JSON, but it won't serialise it properly import marshal type DocItem = object of RootObj tags: seq[string] TextDoc = object of DocItem text: string TodoDoc = object of DocItem todo:…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
3
votes
1 answer

How can I scaffold a new Nim project?

Languages like Rust have Cargo which among other things is used to scaffold a new project by calling cargo new . Is there a similar tool or command in Nim language that can be used to scaffold a new project? Something in the line of…
Amani
  • 16,245
  • 29
  • 103
  • 153
3
votes
1 answer

How do you do string interpolation with variables in Nim?

I've seen that you can interpolate strings with fmt like so: let msg = "hello" echo fmt"{msg}\n" But in my case, the interpolated string is quite long. I would prefer to assign said text to a variable and then the the interpolation later, like…
edmz
  • 3,350
  • 2
  • 22
  • 29
3
votes
1 answer

How do I convert a sequence to an array?

import strutils source = readFile("example.txt") var lines = source.split('\n') I'm reading a file into a string, then splitting it into lines. Is there any way I could convert the sequence to an array so it doesn't take up so much space?
baranskistad
  • 2,176
  • 1
  • 21
  • 42
3
votes
1 answer

Nim cannot open 'opencv/highgui'

I'm trying to use this library https://github.com/dom96/nim-opencv a wrapper for OpenCV. After running nimble install opencv I can see the package in ~/.nimble/pkgs/opencv-0.1.0/ But when attempting to use I get a compile error Error: cannot open…
Lex
  • 4,749
  • 3
  • 45
  • 66
3
votes
1 answer

Rotating around the pivot origin with opengl

I try to implement transforms in my 2D graphics program but I encounter a mystery (even if it's a noob topic, sorry). My example code tries to translates a quad to the center of the screen then rotate by 45 degrees with the center as a pivot but…
Ploumploum
  • 311
  • 1
  • 9
3
votes
1 answer

What is the equivalent of reduce in nim?

Is there a built-in proc which is equivalent to Python reduce or Javascript Array.reduce?
Jundiaius
  • 6,214
  • 3
  • 30
  • 43
3
votes
1 answer

Exporting Nim Anon Function to C++

I am trying to call Nim code from C++. Specifically, a function that takes an anonymous function. I have the following code in Nim: proc test*(a: proc()) {.exportc.} = a() which I compile to a static library. I then link it to my C++ executable and…
Mnenmenth
  • 45
  • 1
  • 7