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
6
votes
3 answers

Creating a fast interpreted language

I'm currently creating a programing language written in Nim. The front-end of the compiler is done, I'm currently sitting in front of a well-built Abstract Syntax Tree (AST) and I tried implementing a simple interpreter, calling an evaluate() method…
Erik Campobadal
  • 867
  • 9
  • 14
6
votes
1 answer

Nim enumerate function like Python

Learning Nim and I like it resemblence of Python (but fast). In Python I can do this: item_index = [(idx, itm) for idx, itm in enumerate(row)] Im looking for a way to enumerate a Nim sequence so I would write this: item_index = lc[(idx, itm) |…
Peheje
  • 12,542
  • 1
  • 21
  • 30
6
votes
1 answer

Nim: advantage of using array over seq?

From the docs, I know that Nim arrays have a fixed length determined at compile time, whereas seqs have a variable length. I notice that seqs have more builtin tools. For example, in the sequtils module, map can take an array but will return a seq…
Adrien
  • 469
  • 3
  • 11
6
votes
2 answers

How to echo/print at compile time in Nim?

When working on compile time features it would be nice to echo something at compile time. If an echo is withing a macro it is already executed at compile time. But is it also possible to print something at compile time e.g. from the global scope?…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
6
votes
3 answers

How to read from the stdin with nim script?

How would i read from the stdin via nimscript? i've tried: if readLine(stdin) == "yes": exec buildCommand i've run the script with nim c build.nims i receive build.nims(50, 13) Error: undeclared identifier: 'stdin'
enthus1ast
  • 2,099
  • 15
  • 22
6
votes
2 answers

Can countup and countdown iterators in Nim language be used in variable declaration?

I am trying to learn Nim and its features, such Iterators; and i found that the following example works fine. for i in countup(1,10): # Or its equivalent 'for i in 1..10:' echo($i) However, The following do not works: var counter =…
GeoObserver
  • 128
  • 6
6
votes
1 answer

How to correctly create a nim/nimrod windows dll

I want to create a dll from nim code. But i failed to register some other exports than "NimMainInner". Even if i try this simple example its not working: proc Hellow(): cint {.exportc.} = echo("hello") return 1 i've compiled it with nim c…
enthus1ast
  • 2,099
  • 15
  • 22
6
votes
4 answers

Unpack multiple variables from sequence

I am expecting the code below to print chr7. import strutils var splitLine = "chr7 127471196 127472363 Pos1 0 +".split() var chrom, startPos, endPos = splitLine[0..2] echo chrom Instead it prints @[chr7, 127471196, 127472363]. Is there a…
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
6
votes
2 answers

Initialize a seq of seqs

I am brand new to Nim and am bumping into some issues. The following code results in SIGSEGV: Illegal storage access. (Attempt to read from nil?). I can't seem to figure out how to populate a sequence of sequences with values. const a = @[ 0, 1, …
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
6
votes
1 answer

Inheriting from sequences in Nim

I've been experimenting with Nim for about a day now and I was wondering how you could make a type inherit from a builtin (seq specifically) so that procedures that operate on seq can also handle the custom type. I've included a minimal example…
megawac
  • 10,953
  • 5
  • 40
  • 61
6
votes
2 answers

How To Convert Slice To Sequence?

I would like to specify a sequence directly from a slice (rather than iterating through the slice and adding each element individually to the sequence). I've tried a few different ways but the obvious ones don't seem to work. var x =…
Matt Smith
  • 425
  • 4
  • 9
6
votes
1 answer

How do you use matrices in Nimrod?

I found this project on GitHub; it was the only search term returned for "nimrod matrix". I took the bare bones of it and changed it a little bit so that it compiled without errors, and then I added the last two lines to build a simple matrix, and…
cjohnson318
  • 3,154
  • 30
  • 33
6
votes
1 answer

How do I parse a string at compile time in Nimrod?

Going through the second part of Nimrod's tutorial I've reached the part were macros are explained. The documentation says they run at compile time, so I thought I could do some parsing of strings to create myself a domain specific language.…
Grzegorz Adam Hankiewicz
  • 7,349
  • 1
  • 36
  • 78
5
votes
2 answers

How to pass command line arguments to a task?

How do I pass command line arguments to a nimble task? For example, say I have the task task mytask, "my task": echo &"my task {args}" When I run nimble mytask --foo --bar I would like nimble to output mytask --foo --bar or something like…
congusbongus
  • 13,359
  • 7
  • 71
  • 99
5
votes
1 answer

Pythonic string.replace() in Nim?

How to replace some string with Nim ? var pythonista:string = "Power of Python!" echo pythonista
How2
  • 365
  • 1
  • 6