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

Go-like parallelism in Nim?

One thing I like in Go and can't seem to find in Nim yet is Go-like, "modified CSP" kind of parallelism. I have not even started learning Nim yet, just considering my options for now. I quite liked the Go model, but Nim seems to have threads…
LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93
7
votes
1 answer

Nim and memory management

I keep reading that Nim's memory management is optional, but documenttion seems to be thin on this, the only resources I have found relate mostly to FFI to C, and https://nim-lang.org/docs/gc.html Is it possible to take control of Nim's memory…
SourceSimian
  • 682
  • 4
  • 18
7
votes
7 answers

How do I convert a string into a sequence of characters in Nim?

I want to do different operations with the characters in a string e.g. map or reverse. As a first step I want to convert the string into a sequence. Given a string like "ab". How do I get a sequence like @['a','b']? "ab".split("") returns the whole…
Sebastian
  • 2,249
  • 17
  • 20
7
votes
2 answers

How can I get the name of procedure in Nim?

I am trying to write a macro for debug print in the Nim language. Currently this macro adds filename andline to the output by instantiationInfo(). import macros macro debugPrint(msg: untyped): typed = result = quote do: let pos =…
mjy
  • 315
  • 2
  • 13
7
votes
2 answers

How to test for an empty seq in Nim?

Let's say I have the following sequences: var s1: seq[int] = @[] var s2: seq[int] var s3: seq[int] = nil var s4: seq[int] = newSeq[int](4) Which of these are typically considered "empty"? And what is the most idiomatic way to test if they are…
Imran
  • 12,950
  • 8
  • 64
  • 79
7
votes
2 answers

What is a baseless method in Nim?

I'm new to the language. When trying to compile a new object type with a method (where the first argument is an instance of my new type), the compiler warned me like this: Warning: use {.base.} for base methods; baseless methods are deprecated…
Antrikshy
  • 2,918
  • 4
  • 31
  • 65
7
votes
2 answers

Basic string formatting with NIM

I am trying to do some very basic string formatting and I got immediately stuck. What is wrong with this code? import strutils import parseopt2 for kind, key, val in getopt(): echo "$1 $2 $3" % [kind, key, val] I get Error: type mismatch: got…
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
7
votes
1 answer

How to load file line by line in Nim?

I want to load a large file line by line in Nim. I tried the following code snippet: for line in lines "largefile.txt": echo line However, this loads the entire file largefile.txt into memory which is not feasible when the file is very large >…
Pythtotic
  • 415
  • 6
  • 10
7
votes
2 answers

How to benchmark few lines of code in nim?

I read this down and up: http://nim-lang.org/docs/times.html But still cannot figure the simple thing: how to get time in ms twice, once before my code, and again after my code runs, and then print the difference? I tried their example: var t0 =…
exebook
  • 32,014
  • 33
  • 141
  • 226
7
votes
1 answer

Run Nim code at compile time

So I know that if I define a const Nim will evaluate whatever I assign to it at compile time, so I could do something like this: proc compileTimeCode: bool = # Put code here return true const _ = compileTimeCode() and then I could put my code…
squirl
  • 1,636
  • 1
  • 16
  • 30
7
votes
2 answers

Wrapping nested templated types in nim

I have a C++ type like: template class Vector { struct Iterator { }; }; And in C++ I can use Iterator as Vector::Iterator. How do I wrap this to use it from Nim? c2nim emits type Vector[T] {.importcpp...} = object type…
dhasenan
  • 1,177
  • 7
  • 15
7
votes
1 answer

passing functions in nim

I'm having issues passing math functions (procs) in Nim (version 0.10.2). import math var s1 = @[1.1, 1.2, 1.3, 1.4] var s2 = map(s1, math.sqrt) I get the error Error: 'sqrt' cannot be passed to a procvar If I write a wrapper function for sqrt,…
COM
  • 847
  • 9
  • 23
7
votes
1 answer

Nim operator overloading

Just started programming in the Nim language (which I really like so far). As a learning exercise I am writing a small matrix library. I have a bunch more code, but I'll just show the part that's relevant to this question. type Matrix*[T; nrows,…
COM
  • 847
  • 9
  • 23
6
votes
1 answer

How to write async code in Nim to be called by timer periodically?

I need to save a cache to disk periodically, every 500ms, how can I do that? The code I tried is not compiled properly. Also, it seems that the asyncCheck should be used for error checking. import tables, os, asyncdispatch var cache =…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
6
votes
1 answer

How do I build Nim library packages

I've created a nimble library package as per the documentation. When I try to build it using nimble build I get the following error. Error: Nothing to build. Did you specify a module to build using the bin key in your .nimble file? I can do this…
Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100