Created Nim for Python Programmers (markdown)

This commit is contained in:
ReneSac 2015-04-03 19:58:32 -03:00
parent 017b7bee99
commit bdaf9d0046
1 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,25 @@
****
**DISCLAIMER!**
Unofficial, work in progress! This is still a stub. Please help extending it.
There may be inaccuracies in this guide.
***
### Inheritance
A Python programmer when first trying Nim can try to write something like this:
``` Nim
# TODO: extend this example with the declaration of the objects w/o ref
var heterogeneousArray: seq[ParentObjectType] = @[]
# TODO: Add an object inheriting from ParentObjectType to the seq
```
That will return a compilation error, because the objects have different sizes. Like in C, you can't make an array of structs with disparate types and sizes. What one can do is an array of references to the objects, all references having the same size put pointing to heterogeneous objects. This is what Python does by default.
``` Nim
# TODO: Show the proper way to do things, with emphasis on what changed
```
So, when using inheritance, be sure to inherit using refs, unless you know what you are doing. Also, check out Nim's generic programming capabilities as an alternative to OOP and inheritance.