nim-wiki/Nim-for-Python-Programmers.md

25 lines
1.0 KiB
Markdown
Raw Normal View History

****
**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.