Moving inheritance section

This commit is contained in:
ReneSac 2015-04-03 21:00:13 -03:00
parent 7d611c591c
commit e81f3fa79d
1 changed files with 20 additions and 22 deletions

View File

@ -6,28 +6,6 @@ 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.
-------
This is a guide for people with experience in Python or a similar language.
The guide assumes some intermediate knowledge.
@ -110,6 +88,26 @@ Nim does not have an implicit _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 Nim. These are at the moment mostly work-in-progress.
### 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.
### Structs - Tuples and Objects
Tuples are entirely different from Python