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

CSRF anti forgery tokens in jester

In nim the jester module does web API servicing, which I'm grateful for, but do we have support of cross site anti forgery tokens ? (against CSRF attacks) like ASP.net provides.
v.oddou
  • 6,476
  • 3
  • 32
  • 63
3
votes
3 answers

How to handle option types in Nim?

Say I have a function with signature proc foo(): Option[int] and I set var x: Option[int] = foo(). How do I perform a different action depending on whether x is some or none? For example in Scala I could do: x match { case Some(a) =>…
Imran
  • 12,950
  • 8
  • 64
  • 79
3
votes
1 answer

What is the !$ (bang dollar) operator in Nim?

In the example of defining a custom hash function on page 114 of Nim in Action, the !$ operator is used to "finalize the computed hash". import tables, hashes type Dog = object name: string proc hash(x: Dog): Hash = result = x.name.hash …
Imran
  • 12,950
  • 8
  • 64
  • 79
3
votes
1 answer

How do I authenticate, using Nim's httpclient module to retrieve HTML?

I'm a beginner and I want to write a Nim-application that processes some data from an internal website. Basic authentication (username, password) is required to access this site. A working Python solution is: response =…
Daniel D.
  • 33
  • 4
3
votes
1 answer

What's the difference between returning void and {.noreturn.}?

In Nim, the noReturn pragma marks a proc that never returns. How is that different than a function that returns void?
dgo.a
  • 2,634
  • 23
  • 35
3
votes
1 answer

How to handle a seq as a return value in Nim

I am running into a problem with Nim sequences and returning them from a function. json_p.nim(42, 33) template/generic instantiation from here json_p.nim(28, 22) Error: no generic parameters allowed for seq Line 28 is where I define my…
wishi
  • 7,188
  • 17
  • 64
  • 103
3
votes
1 answer

How to change string case in NIM?

In NIM 0.17 toLower is deprecated. So, what would be the proper way of changing the case of a string in NIM?
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
3
votes
1 answer

How to iterate over fields (names + types) of a tuple/object in a macro?

I want to write macro which has to perform some logic based on the fields of a named tuple or object. I assume this is best accomplished by passing the tuple/object as a typed parameter to the macro. The question is, how can I iterate over the…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
3
votes
1 answer

How to use Nim's `of` operator with generics?

Consider a type hierarchy, where the base object is non-generic, but sub-types are: type TestBase = ref object of RootObj DerivedA = ref object of TestBase DerivedB[T] = ref object of TestBase field: T proc testProc(x: TestBase) = if x…
bluenote10
  • 23,414
  • 14
  • 122
  • 178
3
votes
1 answer

Load function in Nim dll

I have two files: foo.nim: proc double*(x: cint): cint {.cdecl, exportc: "double", dynlib.} = return x * 2 bar.nim: proc double(x: cint): cint {.cdecl, dynlib: "foo.dll", importc.} echo double(2) I compile foo.nim by running nim c…
pengowen123
  • 1,007
  • 1
  • 12
  • 23
3
votes
1 answer

Is there a Nim library containing an `argsort` implementation or wrapper?

I am looking for a version of argsort, such as exists in numpy or in fortran. Is there an implementation of argsort in Nim ... or accessible to Nim in some library? It seems a bit surprising that it is missing. UPDATE The following seems to work for…
shaunc
  • 5,317
  • 4
  • 43
  • 58
3
votes
1 answer

how to create an IntSet on the heap in nim?

There are various libraries in nim that return actual objects, rather than references. Sometimes I want an object on the heap (regardless of efficiency) -- for instance when I have a generic procedure that expects a ref to an object. The only way to…
shaunc
  • 5,317
  • 4
  • 43
  • 58
3
votes
1 answer

How do I compile a Nim application that runs without a console window?

Is there a way to compile a Nim application that doesn't open a console when it's executed? Unless I'm missing something, the official documentation doesn't show any compiler options that would do this. I really want a program to perform tasks in…
NotAshley
  • 142
  • 2
  • 11
3
votes
1 answer

Reading bytes from many files performance

So I have this code to check for filetype for each file in a directory. Just need to read first 4 bytes and check against pattern. The code looks a little bit convoluted and really slow, but I can't figure out a faster way to do it in Nim. What am…
lqdc
  • 511
  • 2
  • 5
  • 14
3
votes
2 answers

Is it possible / easy to include some mruby in a nim application?

I'm currently trying to learn Nim (it's going slowly - can't devote much time to it). On the other hand, in the interests of getting some working code, I'd like to prototype out sections of a Nim app I'm working on in ruby. Since mruby allows…
user208769
  • 2,216
  • 1
  • 18
  • 27