(Work in progress) This is a guide for people with experience in C or a similar language. The guide assumes some intermediate knowledge, for instance of how stacks and heaps works. The general tutorials can be found here: http://nimrod-lang.org/tut1.html http://nimrod-lang.org/tut2.html The manual provides a more or less complete overview of the language: http://nimrod-lang.org/manual.html ### At a glance
Feature | C | Nimrod |
---|---|---|
Memory Management | Manual (GC w/ libraries or obj-C) | Garbage-collected and manual |
Types | Static | Static |
Compilation | Machine code | Machine code via C (other backends in progress/planned) |
Meta-programming | C Preprocessor | Nimrod (const/when/template/macro) |
Type inference | No (some w/ C++11) | Yes (extensive support) |
Closures | No (Yes w/ obj-C or C++11) | Yes |
Operator Overloading | No (Yes w/ C++) | Yes |
Custom Operators | No | Yes |
C | Nimrod | Comment |
---|---|---|
```C int x; int y = 2; ``` | ```Nimrod var x : int var y1 : int = 2 var y2 = 2 let z = 2 ``` | Define variable. y2 uses type inference. z is single-assignment. In nimrod, uninitialized variables is initialized to 0/nil or similar defaults. |
```C char* s = "Hello World."; char s0 = s[0]; // 'H' char *t = s; // Pointer to s s[11] = '!'; // s and t are both "Hello World." ``` | ```Nimrod var s: string = "Hello World." var s0: char = s[0] # 'H' var t = s # Copy of s s[11] = '!' # s is "Hello World!", t is "Hello World." ``` | Strings and char. Strings are pass-by-value (copied on assignment) and strictly bounds-checked on access. |
```C 9 % 8 // 1 -9 % 8 // -1 (unsigned)(-9) % (unsigned)(8) // 7 ``` | ```Nimrod 9 mod 8 # 1 -9 mod 8 # -1 -9 %% 8 # 7 ``` | Modulo operator. %% treats its argument as unsigned numbers. See |
```C int x = foobar() ? 42 : 0; ``` | ```Nimrod var x = if foobar(): 42 else: 0 ``` | If-statements return the value of the expression they evaluate to, so Nimrod doesn't need a ternary operator. |
```C void foo() { printf("Hello World\n"); } int bar() { return 2; } int baz(int x) { return x*2; } ``` | ```Nimrod proc foo() = echo "Hello World" proc bar() : int = 2 proc baz(x : int) = x*2 ``` | Function/Procedure. |
```C void foobar(person_t *a) { person_t b; b = *a; b.name = "Bob"; *a = b; } ``` | ```Nimrod proc foobar(a: ref TPerson) = var b: TPerson b = a[] b.name = "Bob" a[] = b ``` | Dereference. In C, only the pointer to the strings in the struct is copied. In Nimrod, the string is also copied, but refs are not deep-copied. |