Updated Nimrod for C programmers (markdown)

This commit is contained in:
Araq 2014-12-26 05:38:56 -08:00
parent 7bc9d6e97c
commit 8e07a05706
1 changed files with 28 additions and 28 deletions

View File

@ -10,20 +10,20 @@ There may be inaccuracies in this guide.
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
http://nim-lang.org/tut1.html
http://nim-lang.org/tut2.html
The manual provides a more or less complete overview of the language:
http://nimrod-lang.org/manual.html
http://nim-lang.org/manual.html
### At a glance
Similarities and differences.
Feature | C | Nimrod
Feature | C | Nim
---------------------|-----------------------------------|-----------------------------------------
Compilation | Machine code | Machine code via C*
Meta-programming | C Preprocessor | Nimrod (const/when/template/macro)
Meta-programming | C Preprocessor | Nim (const/when/template/macro)
Memory Management | Manual (GC w/ libraries or obj-C) | Garbage-collected and manual
Types | Static | Static
Dependent types | No | Partial support
@ -46,13 +46,13 @@ Exceptions | No (Yes w/C++) | Yes
### Philosophy
The key to understanding Nimrod is that Nimrod was designed to be as fast as C, but to be much safer. Many of the design-decisions are based on making it harder to shoot yourself in the foot. For example, in C you are required to use a pointer for most of your everyday programming needs. While Nimrod does give you pointers, Nimrod gives you other, safer tools for your everyday needs, while pointers are mostly reserved for interfacing with C and doing low-level system programming. In other words, C gives you a combined hammer and gun, while Nimrod gives you a separate gun and hammer.
The key to understanding Nim is that Nim was designed to be as fast as C, but to be much safer. Many of the design-decisions are based on making it harder to shoot yourself in the foot. For example, in C you are required to use a pointer for most of your everyday programming needs. While Nim does give you pointers, Nim gives you other, safer tools for your everyday needs, while pointers are mostly reserved for interfacing with C and doing low-level system programming. In other words, C gives you a combined hammer and gun, while Nimrod gives you a separate gun and hammer.
The other important thing to know is that while C uses a separate language to do meta-programming (the preprocessor), Nimrod meta-programming is done with the Nimrod language itself. That means that most Nimrod code can be executed at compile time, and Nimrod's ability to generate Nimrod-code at compile time is much more sophisticated.
The other important thing to know is that while C uses a separate language to do meta-programming (the preprocessor), Nim meta-programming is done with the Nim language itself. That means that most Nim code can be executed at compile time, and Nim's ability to generate Nim-code at compile time is much more sophisticated.
### Arrays
In C an array is more or less syntactic sugar for pointers. In Nimrod, arrays are much more strict and safe to use. They are pass-by-value (meaning they're copied at assignment). When passing an array to a proc in Nimrod, the argument is a read-only reference, meaning it can't be assigned to. Take the following example:
In C an array is more or less syntactic sugar for pointers. In Nim, arrays are much more strict and safe to use. They are pass-by-value (meaning they're copied at assignment). When passing an array to a proc in Nim, the argument is a read-only reference, meaning it can't be assigned to. Take the following example:
**C:**
@ -69,7 +69,7 @@ int main() {
}
```
**Nimrod:**
**Nim:**
```Nimrod
proc foobar(z: array[0..3, int]) =
@ -81,49 +81,49 @@ var x = [1, 2, 3, 4]
foobar(x)
```
The C-code will compile, it may or may not crash. The Nimrod code will not compile. If you mean to change the array that was passed to the procedure, you can change the the signature of the procedure to ```proc foobar(z: var array[0..3, int])```. Now you will only get index out of bounds error. If you change the index in both lines to 1, the code will compile. If the index is a variable, Nimrod will include run-time checks on the bounds of the array.
The C-code will compile, it may or may not crash. The Nim code will not compile. If you mean to change the array that was passed to the procedure, you can change the the signature of the procedure to ```proc foobar(z: var array[0..3, int])```. Now you will only get index out of bounds error. If you change the index in both lines to 1, the code will compile. If the index is a variable, Nim will include run-time checks on the bounds of the array.
In C, you can pass an ``int[3]`` to the foobar function, and the compiler will not complain. In this case Nimrod would not compile. You can use an openarray to accept an array of any size, and you can use low(z) and high(z) to query the bounds of the array.
In C, you can pass an ``int[3]`` to the foobar function, and the compiler will not complain. In this case Nim would not compile. You can use an openarray to accept an array of any size, and you can use low(z) and high(z) to query the bounds of the array.
Nimrod arrays can also be indexed from any number. That is, ``z: array[1..4, int]`` is an array of int indexed from 1 to 4. Trying to access ``z[0]`` would throw an index out bounds error.
Nim arrays can also be indexed from any number. That is, ``z: array[1..4, int]`` is an array of int indexed from 1 to 4. Trying to access ``z[0]`` would throw an index out bounds error.
In C, there's nothing that stops you from keeping a pointer to a stack-allocated array after the function that declared it has returned (and the stack is invalidated). In Nimrod, this is true as well, but you are strongly discouraged from using pointers in Nimrod, and you can accomplish almost everything you'd otherwise use pointers for with normal arguments, "var" arguments, variables, and "ref".
In C, there's nothing that stops you from keeping a pointer to a stack-allocated array after the function that declared it has returned (and the stack is invalidated). In Nim, this is true as well, but you are strongly discouraged from using pointers in Nim, and you can accomplish almost everything you'd otherwise use pointers for with normal arguments, "var" arguments, variables, and "ref".
### Unsigned integers
Nimrod strongly discourages the use of unsigned integers, as it's considered unnecessary and somewhat unsafe* for most applications. The unsigned types uint, uint8/16/32/64 are available by default, but the arithmetic and binary functions are not defined. If you do need to do arithmetic on unsigned integers, you need to import the **unsigned** module.
Nim strongly discourages the use of unsigned integers, as it's considered unnecessary and somewhat unsafe* for most applications. The unsigned types uint, uint8/16/32/64 are available by default, but the arithmetic and binary functions are not defined. If you do need to do arithmetic on unsigned integers, you need to import the **unsigned** module.
*See: http://critical.eschertech.com/2010/04/07/danger-unsigned-types-used-here/
### Object-Orientation
Objects in Nimrod have more features than structs in C, but behave quite differently from classes in C++. Objects support inheritance (not multiple inheritance). But otherwise most of the features that apply to objects simply apply to all types in Nimrod.
Objects in Nim have more features than structs in C, but behave quite differently from classes in C++. Objects support inheritance (not multiple inheritance). But otherwise most of the features that apply to objects simply apply to all types in Nim.
You can call a proc on objects with the ```anObject.foobar()```, but you can do that on any type (e.g. ints and arrays) as well. You can have methods on object, but you can have methods on any types, and for all the arguments, not just the first (in C++, implicit) one.
Nimrod does not have an implicit _this_/_self_.
Nim does not have an implicit _this_/_self_.
It is possible to implement object-orientation features from other languages (like C++,Java,etc. or Smalltalk,Obj-C,Ruby,etc.) through libraries, thanks to the extensive meta-programming features of Nimrod. These are at the moment mostly work-in-progress.
It is possible to implement object-orientation features from other languages (like C++,Java,etc. or Smalltalk,Obj-C,Ruby,etc.) through libraries, thanks to the extensive meta-programming features of Nim. These are at the moment mostly work-in-progress.
### Structs - Tuples and Objects
Tuples and Objects in Nimrod are kind of like structs in C, but not really.
Tuples and Objects in Nim are kind of like structs in C, but not really.
### Interfacing C and Nimrod
### Interfacing C and Nim
See [Foreign Function Interface](http://nimrod-lang.org/manual.html#foreign-function-interface)
See [Foreign Function Interface](http://nim-lang.org/manual.html#foreign-function-interface)
### Converting C code to Nimrod
### Converting C code to Nim
See [c2nim](http://nimrod-lang.org/c2nim.html)
See [c2nim](http://nim-lang.org/c2nim.html)
### Cheat Sheet
Note: Code examples are not exactly one-to-one, there may be subtle differences in the semantics. See comments.
<table>
<tr>
<th>C</th><th>Nimrod</th><th>Comment</th>
<th>C</th><th>Nim</th><th>Comment</th>
</tr>
@ -205,7 +205,7 @@ echo "byte ", $int(a),
-9 %% 8 # 7
</pre>
</td>
<td><b>Modulo operator</b>. <i>%% treats its argument as unsigned numbers. <a href="http://nimrod-lang.org/manual.html#pre-defined-integer-types">See</a></i></i>
<td><b>Modulo operator</b>. <i>%% treats its argument as unsigned numbers. <a href="http://nim-lang.org/manual.html#pre-defined-integer-types">See</a></i></i>
</td>
</tr>
@ -223,7 +223,7 @@ var x = if foobar(): 42
</pre>
</td>
<td>
<i>If-statements return the value of the expression they evaluate to, so Nimrod doesn't need a </i><b>ternary operator</b>.</i>
<i>If-statements return the value of the expression they evaluate to, so Nim doesn't need a </i><b>ternary operator</b>.</i>
</td>
</tr>
@ -247,10 +247,10 @@ int baz(int x) {
proc foo() =
echo "Hello World"
proc bar() : int =
proc bar(): int =
2
proc baz(x : int) =
proc baz(x: int) =
x*2
</pre>
</td>
@ -293,7 +293,7 @@ proc foobar(a: ref TPerson) =
a[] = b
</pre>
</td>
<td><b>Dereference.</b> <i>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.</i> </td>
<td><b>Dereference.</b> <i>In C, only the pointer to the strings in the struct is copied. In Nim, the string is also copied, but refs are not deep-copied.</i> </td>
</tr>