From 08f3bf3cbb01a993a58063bc834403acbe2282f0 Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Mon, 6 Apr 2015 19:20:43 +0100 Subject: [PATCH] OO example --- Nim-for-Python-Programmers.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Nim-for-Python-Programmers.md b/Nim-for-Python-Programmers.md index 9129c8e..3ad7e4b 100644 --- a/Nim-for-Python-Programmers.md +++ b/Nim-for-Python-Programmers.md @@ -86,7 +86,26 @@ You can call a proc on objects with the ```anObject.foobar()```, but you can do 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. +It is possible to implement object-orientation features through libraries, thanks to the extensive meta-programming features of Nim. These are at the moment mostly work-in-progress. + +Object orientation example: + +```Nim +type Animal = ref object of RootObj + age: int + name: string + +type Cat = ref object of Animal + playfulness: float + +# A proc that can access and *modify* the object +proc increase_age(self: var Cat) = + self.age.inc() + +var c = Cat(name: "Tom") +c.increase_age() +echo c.name, " ", c.age +``` ### Inheritance