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

Does a statement after defer: get called when breaking iteration early?

For example when iterating over lines of a file using the lines iterator from the standard library, does the file get closed correctly if I break the iteration early using break?
0
votes
1 answer

My Opengl triangle has unexpected vertex colors

Hello I drawn an opengl triangle that worked fine with 3 floats color vertex attributes. The alpha one was in the shader. Now in this version I'm trying to send color attributes with 4 floats. But the colors are wierd and the third vertex is always…
Ploumploum
  • 311
  • 1
  • 9
0
votes
1 answer

How to ignore the result from a procedure in Nim

As per title, assume I have the following: proc squared(n: int64): int64 = n * n Now let's assume I want to call the procedure squared but I am not interested in the result. If I just write: squared(15) I get an error: Error: expression…
norok2
  • 25,683
  • 4
  • 73
  • 99
0
votes
1 answer

R-style logical vector operation in Nim

does anyone know if Nim has a function like 'ifelse' in R that allows to apply conditional calculations on vectors, equivalent to: a<-c(1,2,3,4) b<-c(7,2,7,4) c<-ifelse(a==b,a*b,a+b) using a loop and 'if, else' statement would be too slow for large…
Nimrookie
  • 79
  • 3
0
votes
2 answers

Python HTTPS request SSLError CERTIFICATE_VERIFY_FAILED

PYTHON import requests url = "https://REDACTED/pb/s/api/auth/login" r = requests.post( url, data = { 'username': 'username', 'password': 'password' } ) NIM import httpclient, json let client =…
UrbKr
  • 621
  • 2
  • 11
  • 27
0
votes
2 answers

Can't call Nim dll in NodeJS

I have a C library clib.c with this function int hi(char* hello) { return 900; } compiled as: gcc clib.c -o clib.so --shared -fPIC I'm consuming this in a Nim libray called 'nlib.nim`: proc hi*(hello: cstring): cint {.cdecl, importc: "hi", dynlib:…
David Broderick
  • 112
  • 1
  • 8
0
votes
1 answer

Cryptopals 1.2 in Nim: XOR returns an unexpected value

I thought doing the Cryptopals exercises in Nim would be a nice way to learn the language. On set 1 exercise 2, I'm getting an unexpected result on the xor part. The exercise: If your function works properly, then when you feed it the…
filipe
  • 414
  • 1
  • 5
  • 14
0
votes
1 answer

Softlayer API:how to get all Virtual_Guest config types that we can use with softlayer apis

When I want to upgrade/downgrade virtual guest or create a new virtual guest, we need some configs options, but I don't know how to get these available configs? On the same time, how about disk and network?
JerryZ
  • 5
  • 2
0
votes
1 answer

User defined type as reference

Are these two definitions equivalent? type PersonObj = object name: string age: int PersonRef = ref PersonObj type PersonObj = ref object name: string age: int In the latter should PersonObj be simply called Person?
Facorazza
  • 317
  • 1
  • 15
0
votes
1 answer

I can't use twitter api with Nim

I want to use twitter api with Nim. But, I can't solve error. {"errors":[{"code":85,"message":"The list failed validation: A list's name can't be blank."}]} I success authentication. Which I make a mistake using twitter API or using Nim library…
leaf_chage
  • 11
  • 2
0
votes
1 answer

Why does removing a declared but not used variable causes program to terminate early?

I cannot figure this one out. When I remove let size = s.readInt64() from the following proc, the .exe seems to terminate before it reaches the end. It is declared but not used! Its gotta go! proc load(fn: string): Alternating = var s =…
Joshua Fenner
  • 355
  • 1
  • 7
0
votes
1 answer

statement not allowed after 'return', 'break', 'raise', 'continue'

proc myproc(T: typedesc): string = when T is bool: return "bool" when T is float: return "float" echo myproc(bool) Error: statement not allowed after 'return', 'break', 'raise', 'continue' or proc call with noreturn pragma using…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
0
votes
1 answer

do notation magically fixes an otherwise refused expression as argument

In Nim templates: as a follow-up from this problem, I had the idea of working around the default-argument unavailability by using overloading, or even in this case, plain wrapping. Though, it would be too good if something didn't hit the fan again,…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
0
votes
1 answer

Error: expression 'none(int)' is of type 'Option[system.int]' and has to be discarded

import options template p[T] = none(T) discard p[int] templat.nim(5, 10) Error: expression 'none(int)' is of type 'Option[system.int]' and has to be discarded I think writing discard in front of the template instantiation is a reasonable…
v.oddou
  • 6,476
  • 3
  • 32
  • 63
0
votes
1 answer

substring in string in nim (in operator)

I wanted to verify substring existence in a string and came up with: if "haystack".find("needle") != -1: But I would have prefered: if "needle" in "haystack": as in python. Though we get: Error: type mismatch: got (string, string) but expected one…
v.oddou
  • 6,476
  • 3
  • 32
  • 63