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

How do I execute a proc with a delay in nim?

I was looking to get a bit more into async in nim with the C-backend and have a fair bit of experience with Javascript. There functionality such as setTimeout is available to execute a function at a later time. Example: setTimeout(() =>…
Philipp Doerner
  • 1,090
  • 7
  • 24
2
votes
2 answers

Translation in Happyx

Can I translate strings in happyx web framework? For example ... get "/": if lang == "en": return "Hello, world!" else: return "Bye" Now I'm trying to use request headers to detect language, but can I made it otherwise?
2
votes
2 answers

Semantics of `let` assignment in Nim

Semantics of let assignment in Nim I was recommended the Nim programming language and started reading through the Tutorial 1, the Manual and even the memory model (in particular the section about strings and seqs). I soon encountered some strange…
Federico
  • 582
  • 3
  • 12
2
votes
2 answers

black_box analogue in Nim

I've been trying to benchmark a function, but the C compiler optimizes the unused variable with placeholder syntax out: let start = getTime() let _ = fn(x) let elapsed = inNanoseconds(getTime() - start) in this example elapsed equals zero. It might…
Miiao
  • 751
  • 1
  • 8
2
votes
1 answer

How to print Nim's AST at runtime?

dumpTree and similar macros print Nim's AST at compile time. I was wondering how to print the same content at runtime. The use case is to be able to capture the output to document it in a nimib document. To have a concrete example: import…
pietroppeter
  • 1,433
  • 13
  • 30
2
votes
0 answers

Trying to create a Nim-Nim dll, to be able to work with plugins, but tests keep crashing

Creating a Nim only dll is hardly documented. I'm creating an addon for a simulator (3rd party) with plugins based on the spacecraft being flown. Every 'spacecraft' module would only have 3 procedures that can be called by the main speech…
IvanS
  • 31
  • 4
2
votes
1 answer

Nim import from project root directory

How to import a module from project root directory in Nim? Instead of doing so: import ../../http import ../seq I'd like to import my module using absolute path relative to the project root directory, like this: import src/http import…
Alexandre Daubricourt
  • 3,323
  • 1
  • 34
  • 33
2
votes
1 answer

How to access elements of pointer array in Nim?

I've been stuck on this let aiArray: ptr aiMesh = sceneptr[].mMeshes[] Here aiArray is a pointer to an array (from C). How can I access the elements like aiArray[0], aiArray[1] and stuff? I tried doing the aiArray[0] thing but it didn't work. I…
2
votes
1 answer

Nim: How can I improve concurrent async response time and quota to match cpythons asyncio?

For an upcoming project this year, I wanted to look into some languages that I haven't really used yet, but that repeatedly catch my interest. Nim is one of them . I wrote the following code to make async requests: import asyncdispatch, httpclient,…
tenxsoydev
  • 370
  • 2
  • 10
2
votes
1 answer

How to catch the output of a compiler error in nim?

I am not sure if this is currently possible (and maybe it is not even advisable), but I would like to be able to catch the output of a compiler error and reuse it in code. An example would be: type IntOrString = int | string var s:…
pietroppeter
  • 1,433
  • 13
  • 30
2
votes
1 answer

Override string conversion for custom classes in Nim

Is it possible to override whatever method is used when an object of a custom class is converted to a string for display? For example, this code currently prints (x: 4, y: 5), but I want it to print just (4,5) type Point = object x: int y:…
Saeed Baig
  • 1,197
  • 12
  • 13
2
votes
1 answer

How to pass file lines as argument in Nim?

I’m learning Nim and I have a simple program that reads lines of a file. I would like to make it testable by extracting the main code in a function that takes an iterable of lines: func countLines(lines: iterator): int = var n = 0 for _ in…
bfontaine
  • 18,169
  • 13
  • 73
  • 107
2
votes
1 answer

How to dynamically link your nim-application against musl?

I've written a web-server in nim using the prologue framework. I would like to deploy that application using an alpine-docker-container. As far as I'm aware, compiling means you dynamically link against your system libraries for whatever you need,…
Philipp Doerner
  • 1,090
  • 7
  • 24
2
votes
2 answers

Using Nim to Creating a vb6 dll that Returns String

Nim Compiler Version 1.6.6 [Windows: i386] Compiled at 2022-05-05 Copyright (c) 2006-2021 by Andreas Rumpf active boot switches: -d:release Cmd Compile nim c --cpu:i386 -d:release --app:lib --nomain mydll.nim Hi there, I was able to return a Long…
huvdev4
  • 61
  • 5
2
votes
2 answers

enums in nim for wrapper of c library

I am trying to create a simple nim wrapper around the Clever Audio Plugin c library. In c there is an enum of format flags that can be activated using bitwise operations. summary of the c code # definitions enum clap_note_dialect { …
Alex Gustafson
  • 198
  • 1
  • 8