Skip to content

Commit

Permalink
Cleanup a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
jyapayne committed Jun 21, 2018
1 parent ca83d7a commit 92a16b3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 61 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# nim-extensions
Extensions for the nim programming language.
Extensions for the Nim programming language.

These extensions aim to improve the usability of nim in practical applications.
These extensions aim to improve the usability of Nim in practical applications.

Extensions so far include:

Expand All @@ -12,7 +12,7 @@ Extensions so far include:
usage:

```nim
import oop_macro
import extensions/oop
class BaseObject: # inherits from RootObj
# attributes/properties
var
Expand All @@ -33,5 +33,5 @@ Extensions so far include:

There's also an example in the code that demonstrates the inheritance that can be run by executing:
```bash
nim c -r oop_macro.nim
nim c -r tests/test1.nim
```
59 changes: 3 additions & 56 deletions src/extensions/oop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import strutils

{.hint[XDeclaredButNotUsed]: off.}

macro new*(obj: untyped): untyped {.immediate.}=
macro new*(obj: untyped): untyped =
if obj.kind == nnkObjConstr or obj.kind == nnkCall:
var args: seq[NimNode] = @[]
var newObj = copyNimTree(obj)
Expand Down Expand Up @@ -45,7 +45,7 @@ macro new*(obj: untyped): untyped {.immediate.}=


macro class*(head: untyped, body: untyped): untyped =

# object reference name inside methods.
# ie: self, self
let objReference = "self"
Expand Down Expand Up @@ -288,58 +288,5 @@ macro class*(head: untyped, body: untyped): untyped =
# OfInherit
# Ident !"RootObj"
# Empty <= We want to replace self
typeDecl#[[0]]#[0][2][0][2] = recList
typeDecl[0][2][0][2] = recList
result.insert(0, typeDecl)

when isMainModule:
class Animal of RootObj:
var
name: string
age: int

method init*(name: string, age: int)=
self.name = name
self.age = age
echo "I am a new Animal, ", self.name

method stuff(s:string): string = s
method vocalize: string = "..."
method ageHumanYrs: int = self.age # `self` is injected

class Dog of Animal:
method vocalize: string = "woof"
method ageHumanYrs: int = self.age * 7

class Cat of Animal:
method vocalize: string =
# call the base class method
self.vocalizeAnimal() & "meow"

class Tiger of Cat:
method init(name: string="Bob", age: int)=
self.initAnimal(name, age)
echo "I am a new tiger"
method vocalize: string =
# no need for super.super!
self.vocalizeAnimal() & "Rawr!"

var animals: seq[Animal] = @[]
animals.add(new Dog(name: "Sparky", age: 10))
animals.add(new Cat(name: "Mitten", age: 10))
animals.add(new Tiger(name: "Jean", age: 2))

for a in animals:
echo a.name, " says ", a.vocalize()
echo a.ageHumanYrs()

# prints:
# I am a new Animal, Sparky
# I am a new Animal, Mitten
# I am a new Animal, Jean
# I am a new tiger
# Sparky says woof
# 70
# Mitten says ...meow
# 10
# Jean says ...Rawr!
# 2
2 changes: 1 addition & 1 deletion tests/test1.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import extensions/oop_macro
import extensions/oop

import strutils

Expand Down

0 comments on commit 92a16b3

Please sign in to comment.