Created Nim for Python Programmers (markdown)
This commit is contained in:
parent
017b7bee99
commit
bdaf9d0046
|
@ -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.
|
Loading…
Reference in New Issue