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

Why I can't apply => to function with void?

Sugar works with map import sugar proc map2*[T, R](list: openArray[T]; cb: proc (x: T): R): seq[R] = for v in list: result.add(cb(v)) result echo @[1, 2].map2((v) => v*v) But not with each, seems like it somehow related to void, is there a…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
2 answers

Does Nim support autocasting?

I'm trying to call function on objects of specific type from the sequence, but compiler complains. How to fix that? I need to get text property from the TextDoc objects. type DocItem = object of RootObj tags: seq[string] TextDoc = object of…
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

Is it possible to use multiple top-level source directories in Nimble?

By default Nimble assumes that the sources are in single /my_project/src folder. Is it possible to configure it so that there will be multiple top-level source folders? /my_project /lib # source directory 1 /support # source directory 2 …
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

How to retrieve a node by label in a "grim" graph?

I'd like to use the grim library to create and traverse graphs, but I don't understand how to access a graph having the label in a string. import grim import sequtils var g = newGraph("my graph") let node = g.addNode("F4", %(Id: "none", desc:…
Andrea T.
  • 920
  • 4
  • 15
0
votes
1 answer

Why can't I call Nim proc without braces?

Nim support proc calling expression without braces, but when I use named arguments it complains, why? proc doc(text: string) {.discardable.} = echo text doc "doc1" doc(text = "doc1") doc text = "doc1" # <== Error here
Alex Craft
  • 13,598
  • 11
  • 69
  • 133
0
votes
1 answer

Exit console application in Nim

I would like to know how to exit the console application in Nim.
Ducklett
  • 63
  • 5
0
votes
1 answer

Is there an example of implementing SQLite3 extensions in nim-lang?

Here is my sample code. It kinda works but there is a problem. The SQLite3 API for creating a function includes a prototype that described the return params. However, the nim-wrapper does not. The example below compiles and executes, however, since…
Richard
  • 10,122
  • 10
  • 42
  • 61
0
votes
1 answer

Nim: Can't access fields of object returned by a procedure

I was trying to write a lexer in Nim. Sorry, if this sounds a bit idiotic because I started using nim yesterday, so my problem is that I created a type such as what follows import position type Error* = object pos_start : position.Position …
0
votes
1 answer

How can I force all fileds in struct to be filled like constructor in OOP?

Abstract & Codes I'm learning Golang and Nim which don't have class but has struct, and new to these kind of language. Being used to OOP I tried to make a class-like thing using struct but found that struct's fields are optional. I want to turn them…
Takuya HARA
  • 499
  • 1
  • 3
  • 17
0
votes
1 answer

first `readLine` is skipped inside a `case - of` control flow in Nim-lang

I have the following code. import lib var stat = false when isMainModule: while stat != true: echo("Option: ") var opt = readChar(stdin) case opt of 'q': stat = true of 'n': echo("Salu: ") var ss…
Bussller
  • 1,961
  • 6
  • 36
  • 50
0
votes
2 answers

How to write a text prompt in Nim that has readline-style line editing?

readLine() doesn't support line editing and recalling previous commands, eg: while true: var name: string = readLine(stdin) echo "Hi, ", name, "!" Has no editing. But if I compile that and wrap it in rlwrap: $ rlwrap read_test It works as I…
0atman
  • 3,298
  • 4
  • 30
  • 46
0
votes
2 answers

How can i echo a string with escape sequences in Nim?

If I echo a string like this: let s = "Hello\nworld" echo s I get: Hello world I would like to output: Hello\nworld I know I can use raw string literals if I'm defining the string, but how do i do it if the string comes from, for example, a…
SimplyZ
  • 898
  • 2
  • 9
  • 22
0
votes
1 answer

Stable matching matches least preferable

I have implemented the stable matching algorithm but it seems that the implementation matches the least preferable candidates. I'm asking for a code review: import options, sequtils type Person* = ref object id: int name:…
noconst
  • 639
  • 4
  • 15
0
votes
1 answer

Nimlang: Async program does not compile

I'm trying to write a HTTP server that sends a HTTP request and returns the content to client. Here is the code: import asynchttpserver, asyncdispatch import httpClient let client = newHttpClient() var server = newAsyncHttpServer() proc cb(req:…
Pythtotic
  • 415
  • 6
  • 10
0
votes
1 answer

How to call string function in DotNet Core with ImportDll in nim-lang

I wanted to use Nim inside my C# code, and I used this https://www.euantorano.co.uk/posts/nim-with-c-sharp-net/ example with no issue. Okay I said, lets get a string proc working. proc HelloNim*(a, b: cint): cstring {.cdecl, exportc, dynlib.} = …
SlightlyKosumi
  • 701
  • 2
  • 8
  • 24