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

How to get reference to proc self inside multi-dispatch proc?

Is it possible to get a reference to proc by its symbol for multi-dispatch proc? I need to auto-generate some code based on function signature, and can't get the reference to the function. run (seems like it's not showing output in the online…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
2 answers

How to make pragma to work like a standalone macro?

How can I make macro that works when used as pragma to also work if used as plain call? Please note that it has to be untyped as for some reason I can't use typed macro. Run import macros proc fn_signature(fn: NimNode): string = let fn_impl =…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

how can i include a file via a variables value

I want to set a variable to the file path to include and then use that variable to include it I tried: var path: string = "example.nim" include path This gives an error because it thinks the path I'm trying to include is "path" Basically I want to…
0
votes
1 answer

Macro to generate function implementation by its definition?

Would it be possible to automate writing the implementation for remote function? If written by hand it would look like code below. Having both function declaration and implementation is important. proc rcall*[A, B, R](fn: string, a: A, b: B, _:…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

Cube not appearing in OpenGL

I am trying to make a video game using Nim and OpenGL. I am using GLFW. I have some code to make a cube meant to be grass. However, when I run my program, I see a light blue background, but no cube. I have tried moving the camera around to different…
0
votes
2 answers

How to normalise file path in Nim?

The os.normalized_path outputs . instead of the full path. import os echo ".".normalized_path
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

My code is producing an error when run asynchronously but it works synchronously

I am making a 3d video game with OpenGL with Nim. Originally, I had my code to draw some cubes in my display function, but it uses the {.cdecl.} pragma, meaning I cannot access external variables like the camera position. I switched to an empty…
0
votes
2 answers

How to write macro list.findBy(key, value) in Nim?

Would it be possible to write list.findBy(key, value) macro, so that: let people = @[(name: "John"), (name: "Sarah")] echo people.findBy("name", "John") Ideally it should validate "name" at compile time. I tried some code, but it doesn't work,…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

Nim template where early binding is not possible?

There's the build_type template defined in the lib.nim library. template build_type*[T](_: type[T]): T = T.build() The object B uses that template to build object A. And it doesn't work - because while A is visible in the b.nim, it is not visible…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

Does value/reference types in Nim works same way as in Swift?

I'm trying to better understand reasoning behind the value types. Does value/reference types in Nim works same way as in Swift?
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
2 answers

I installed Nim without any problem, but it gives me this error, what should I do?

I install Nim Lang but it not works. I get this errors, how can i fix? (I have completed the setup.) Error: invocation of external compiler program failed. Sistem belirtilen dosayayi bulamiyor. Additional info: "Requested command not found:…
SyTax
  • 1
0
votes
2 answers

Where to program with Nim (linux)?

Just finished full installation of Nim language to my Debian GNU/linux and feel a bit confused, not finding any development kits or at least console commands to try some scripting... Isn't it supposed to be included into instalation packages? So…
Deno
  • 57
  • 5
0
votes
1 answer

What is the significance of uppercase file names?

I'm trying to implement a "static" function in Nim by doing this: # File: OtherObject.nim type OtherObject* = ref object of RootObj proc mystatic*(_: typedesc[OtherObject]) = echo "Hi" # File: test.nim import…
jjv360
  • 4,120
  • 3
  • 23
  • 37
0
votes
2 answers

How can I pass subclass to proc expecting argument of parent type?

It's possible to manually convert seq[Child] to seq[Parent] but maybe there's a better option? type ParentRef = ref object of RootObj a: int ChildRef = ref object of ParentRef b: int let parents = @[ParentRef()] let children =…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

How can I get element by the Index from the OrderedTable in Nim?

Nim has OrderedTable how can I get the element by its index, and not by its key? If it's possible - is this an efficient operation, like O(log n) or better? import tables let map = {"a": 1, "b": 2, "c": 3}.toOrderedTable Something…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133