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

What name does this syntax have

In the language nim, one can do the following thing: let num = 5.add(3) which would be the same as let num = add(5,3) So, basically you take the expression before the dot as the first argument to the function. I'm sure other languages have this…
Dirk
  • 2,094
  • 3
  • 25
  • 28
4
votes
1 answer

Undeclared field in inherited type compiling Nimrod program

I try to compile the following code: type TPerson = object of TObject name*: string age: int TStudent = object of TPerson id: int var student: TStudent person: TPerson student = TStudent(name: "Anton", age: 5, id: 2) but I…
sloth
  • 99,095
  • 21
  • 171
  • 219
3
votes
1 answer

How to get all fields of an object type `MyType` in a macro from a NimNode ident("MyType")

In nim I have a macro that takes in a proc definition and generates a proc based on that and some statements. The syntax looks like this: type A = object name: string type B = object name: string id: int macro generateMapper(body: untyped):…
Philipp Doerner
  • 1,090
  • 7
  • 24
3
votes
2 answers

unable to share a proc between two classes

I am playing around with NIM and classes. Ima trying ti have one i it function for two classes, however it seems that this result in the initialization nit to be possible. The code looks like this type Attribute* = object field: float type…
Francesco
  • 481
  • 2
  • 4
  • 16
3
votes
2 answers

Nim: read all content of text file

I liked to read the whole content of a text file using Nim. I tried let fileContent: string = readAll("file.txt") but this doesn't compile.
Quonux
  • 2,975
  • 1
  • 24
  • 32
3
votes
1 answer

How to enable --threads:on only for files using it?

In a project producing multiple binaries, where only some of them are importing a library (in this case "threadpool") which requires --threads:on to be passed to the compiler, what would the way to handle this? In my nimble file I have something…
Andrea T.
  • 920
  • 4
  • 15
3
votes
1 answer

How to iterate over a compile-time seq in a manner that unrolls the loop?

I have a sequence of values that I know at compile-time, for example: const x: seq[string] = @["s1", "s2", "s3"] I want to loop over that seq in a manner that keeps the variable a static string instead of a string as I intend to use these strings…
3
votes
1 answer

Function comparing two strings slower in nim than python

I am a novice in nim, but want to use it to write functions to use in python. I am using nimpy and nimporter for importing nim functions to python. This function in python: def pcompare(a, b): letters = [] for i, letter in enumerate(a): …
3
votes
1 answer

Add the ability to use tabulation in Nim

Yes, we can convert tabs to spaces via Sublime, VS-Code and etc, it's not a big problem. But what if I want to get rid off this additional action ? Found answer by adding this line to .nim file : #? replace(sub = "\t", by = " ") My additional…
How2
  • 365
  • 1
  • 6
3
votes
1 answer

Using Nim to generate a DLL for use in C#/VB6

After many hours of trying to figure out this problem I'm wondering if there even is a way of doing it. I need to be able to export functions that are called from VB6 (yes you read that correctly, not VB.NET) and I'm at a loss. I got as far as…
SLWW
  • 89
  • 7
3
votes
1 answer

Import a specific macro from a module in Nim

I import the json module just to use its useful %* macro: import json # for %* let json_payload = $(%* {"username": "admin", "password": "1234"}) Is it possible to import just this particular macro from the module? Something like this…
Saeed Baig
  • 1,197
  • 12
  • 13
3
votes
0 answers

Nim-Lang compiler error with leading @ character

I just learn to work with Nim-Lang. Im running Nim under Windows 10. Compiling my first test program worked well. Also using it with nimpy as a library in Python. Now I generated a build script using the options --genScript:on --nimcache:./nimcache.…
Michael Hecht
  • 2,093
  • 6
  • 25
  • 37
3
votes
1 answer

Is there any way you can import local procs from other files in the same directory?

If you have 2 Python files in the same directory, you can do the following: something.py def returnSomething(): return True index.py from something import returnSomething test=returnSomething() Can something similar be done in Nim by…
Anthony
  • 33
  • 1
  • 3
3
votes
2 answers

How to mark non-pure function as pure in Nim

Is there a way to mark non-pure function p as pure? Maybe with some pragma? I'm using p for debug, and it can't be used inside pure func procs. playground proc p(message: string): void = echo message func purefn: void = p "track" …
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
3
votes
1 answer

How to write function similar to `echo` in Nim?

I'm trying to write wrapper for echo the code below doesn't work, playground import sequtils, strutils, sugar proc p*(args: varargs[typed, `$`]): void = echo args.map((v) => $v).join(" ") Error: /usercode/in.nim(3, 8) Error: invalid type:…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133