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

c struct instantiation in c2nim wrapper

I am trying to put together a nim wrapper for the clap AudioPlugin API. I am using scripts to try and automate as much of the conversion as possible using c2nim to do the converting. I realize that some stuff will need to be tweaked by hand, but I…
Alex Gustafson
  • 198
  • 1
  • 8
0
votes
1 answer

.exe has stopped working when use thread module

I just copy the code from the example on nim's offical documentation: import std/locks var thr: array[0..4, Thread[tuple[a,b: int]]] L: Lock proc threadFunc(interval: tuple[a,b: int]) {.thread.} = for i in interval.a..interval.b: …
LingC
  • 1
  • 2
0
votes
1 answer

How to read in files with a specific file ending at compile time in nim?

I am working on a desktop application using nim's webgui package, which sort of works like electron in that it renders a gui using HTML + CSS + JS. However, instead of bundling its own browser and having a backend in node, it uses the browser…
Philipp Doerner
  • 1,090
  • 7
  • 24
0
votes
1 answer

Can't evaluate at compile time - NIM

Hi I'm starting to play around with NIM I get a "can't evaluate at compile time" error on this code: import strutils type Matrix[x, y: static[int], T] = object data: array[x * y, T] var n,m: int = 0 proc readFile() = let f =…
dromologue
  • 41
  • 2
0
votes
2 answers

How do I compile a binary with a const string in debug and change it in release?

I would like to create a binary that is storing some configuration data that will be supplied at compile time and built into the binary that I don't want present in the release versions. In C I would do this like #ifdef DEBUG #define LOOKUP_TABLE…
Hortinstein
  • 2,667
  • 1
  • 22
  • 22
0
votes
1 answer

Conditional type in Nim. How to get half unsigned int type from a int function parameter?

See bellow sample code. Please help. Thanks! Blow is non working Nim version sample code. macro GetHalfUInt(anyInt : untyped ): untyped = when sizeof(anyInt) == 8: uint32 else when sizeof(anyInt) == 4: uint16 else when sizeof(anyInt)…
azyx
  • 35
  • 3
0
votes
1 answer

NIM string to seq[string]

Can anyone please assist I'm lost? I have a string converted to string that I need to convert to seq[string] simple example given below: vat string1 = "this is a string" var cmd: seq[string] How do I get the string1 to be converted or cast into my…
testuser
  • 1
  • 1
0
votes
1 answer

SMTP - 503 Bad sequence of commands when using nim's std/smtp

I am building a web application in the nim programming language and recently wanted to start implementing features regarding sending mails. Nim has a library std/smtp for this that comes with some pretty simple examples for using both starttls and…
Philipp Doerner
  • 1,090
  • 7
  • 24
0
votes
1 answer

initialize an argument of a proc, but not in it's definition line

Suppose that we have an object which has some properties of type proc: type x = object y: proc(a,b:int) proc myproc(a,b:int) = echo a var tmp = new x tmp.y = myproc # I want to insert initial value in this line for example a =…
Shoaib Mirzaei
  • 512
  • 4
  • 11
0
votes
1 answer

realign controls inside a layout in NiGui

I am new to Nim and NiGui. It seems that NiGui does not have a grid container. So I decided to write one for It. The grid can have either fixed or varying number of rows and columns. Creating such a container is not very hard, but sometimes it is…
Shoaib Mirzaei
  • 512
  • 4
  • 11
0
votes
1 answer

Jsony newHook has `SIGSEGV: Illegal storage access. (Attempt to read from nil?)` when deserializing into ref-objects

I am writing a web-application and am deserializing via jsony into norm-model-object types. Norm-model-types are always ref objects. Somehow my code which is very similar to the default example in jsony's github documentation does not compile.…
0
votes
1 answer

How do I call Mix_GetError from the nim SDL2 Mixer bindings?

There are multiple references in the nim SDL2 mixer.nim file to Error messages can be retrieved from Mix_GetError(). However, I can't seem to find this function defined in the mixer.nim file How can I get the latest error string?
lilroo
  • 2,928
  • 7
  • 25
  • 34
0
votes
1 answer

How do I access an object field via its field name in string form?

I'm figuring out at compile-time the name of a specific field of an object that I want to access. Before I compile, I do not necessarily know which field that is going to be, so I just have that field name as a string. How can I leverage nim's…
Philipp Doerner
  • 1,090
  • 7
  • 24
0
votes
1 answer

how to pass +, -, etc. to macro in Nim

I want to do something like this macro in Nim #define BINARY_OP(op) \ do { \ double left = getLast(); \ double right = getLast(); \ push(right op left); \ } while (false) I tried to do this: macro binaryOp(op: untyped) = …
pooooky
  • 490
  • 2
  • 12
0
votes
1 answer

Norm - Querying entries within generics causes type mismatch compilation error

I write a web application in the nim programming language and use norm as my ORM. There I follow the standard repository pattern, meaning you have associated with a given URL a controller which calls services that contain business logic on what…
Philipp Doerner
  • 1,090
  • 7
  • 24