Skip to content

Commit

Permalink
Bug with default constructors in proxied class
Browse files Browse the repository at this point in the history
  • Loading branch information
jjv360 committed Dec 27, 2021
1 parent 79a36bc commit 3c1ddc4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion classes.nimble
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Package
version = "0.2.6"
version = "0.2.7"
author = "jjv360"
description = "Adds class support to Nim."
license = "MIT"
Expand Down
10 changes: 6 additions & 4 deletions src/classes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,20 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS


# Add default constructors for all constructors that don't exist in the subclass but do in the parent class
for methodNode in parentClassInfo.methodDefs:
for methodNode2 in parentClassInfo.methodDefs:

# Allow methodNode to be captured
let methodNode = methodNode2

# Stop if not an init
if $methodNode.name != "init":
continue

# Check if this method signature exists
var didExist = false
for node in body:
traverseNodeStatements body, proc(idx: int, parent: NimNode, node: NimNode) =
if node.kind == nnkMethodDef and $node.name == "init" and node.params.repr == methodNode.params.repr:
didExist = true
break

# Stop if exists
if didExist:
Expand Down Expand Up @@ -768,7 +770,7 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS
)

# if $className == "AsyncCls":
# echo result.repr
echo result.repr

# Export new keyword which was imported from our lib
# let newIdent = ident"new"
Expand Down
43 changes: 43 additions & 0 deletions test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ assert(ClassWith3Init.init(5, 5, 5, e=4.5, d=3.4).v1 == 15)



test "Constructor inheritance"

class ClsInit1:
var v1 = 1
method init() = this.v1 += 2

class ClsInit2 of ClsInit1:
method init() =
super.init()
this.v1 += 3

class ClsInit3 of ClsInit2:
method init() =
super.init()
this.v1 += 4

assert(ClsInit3.init().v1 == 10)



group "Destructors"
test "Called on dealloc"
warn "Not implemented yet"
Expand Down Expand Up @@ -347,6 +367,29 @@ assert(MyProxiedClass.init().injectedVar == 3)
assert(MyProxiedClass.init().injectedMethod() == 5)
assert(MyProxiedClass.init().originalVar == 6)

class2 ProxyClass2 of MyProxiedClass:
var v3 = 3
method m4(): int = 4

assert(ProxyClass2.init().v3 == 3)
assert(ProxyClass2.init().m4 == 4)

class2 Cls2Init1:
var v1 = 1
method init() = this.v1 += 2

class2 Cls2Init2 of Cls2Init1:
method init() =
super.init()
this.v1 += 3

class2 Cls2Init3 of Cls2Init2:
method init() =
super.init()
this.v1 += 4

assert(Cls2Init3.init().v1 == 10)



group "Mixins"
Expand Down

0 comments on commit 3c1ddc4

Please sign in to comment.