From 1dae01b3d045bea000aa8a9fce46e23102988561 Mon Sep 17 00:00:00 2001 From: Josh Fox Date: Thu, 26 Jan 2023 10:38:10 +0200 Subject: [PATCH] Fix constructor ordering --- classes.nimble | 2 +- src/classes.nim | 19 +++++++++---------- test.nim | 13 +++++++++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/classes.nimble b/classes.nimble index b0ade87..e32cc53 100644 --- a/classes.nimble +++ b/classes.nimble @@ -1,5 +1,5 @@ # Package -version = "0.2.13" +version = "0.2.14" author = "jjv360" description = "Adds class support to Nim." license = "MIT" diff --git a/src/classes.nim b/src/classes.nim index 152d337..816f0ee 100644 --- a/src/classes.nim +++ b/src/classes.nim @@ -377,6 +377,7 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS # Add forward declarations, so that the order of the methods doesn't matter + var lastForwardDeclarationIndex = 1 traverseClassStatementList body, proc(idx: int, parent: NimNode, node: NimNode) = # We only care about method definitions right now @@ -433,6 +434,7 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS # Add it if showDebugInfo: echo "Adding declaration for " & (if isStatic: "static " else: "") & "method: name=" & $methodNode.name & " args=" & $(methodNode.params.len()-2) result.add(methodNode) + lastForwardDeclarationIndex = result.len @@ -526,8 +528,8 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS # Add to template's function call newFunc[6][0].add(paramIdent) - # Done, add it - result.add(newFunc) + # Done, insert it after the forward declarations + result.insert(lastForwardDeclarationIndex, newFunc) # If this is an init method, add a Class.init() wrapper function for it. # NOTE: We can use this for static methods! @@ -564,9 +566,8 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS # Add to template's function call newFunc[6][0].add(paramIdent) - # Done, add it - result.add(newFunc) - # echo newFunc.repr + # Done, insert it after the forward declarations + result.insert(lastForwardDeclarationIndex, newFunc) # If this is an init method, add a Class.new() wrapper function for it. if $methodNode.name == "init": @@ -607,10 +608,8 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS # Add to template's function call newFunc[6][1][0].add(paramIdent) - # Done, add it - result.add(newFunc) - # echo newFunc.repr - # if $className == "DialerWindow": quit() + # Done, insert it after the forward declarations + result.insert(lastForwardDeclarationIndex, newFunc) # Check if it's static ... would have been nice to use hasCustomPragma() here? var isStatic = false @@ -770,7 +769,7 @@ proc createClassStructure(head: NimNode, bodyNode: NimNode, result: NimNode, isS return `sharedVarName` ) - # if $className == "ExternalClass": + # if $className == "ClassConstr1": # echo result.repr # Export new keyword which was imported from our lib diff --git a/test.nim b/test.nim index fa457ff..369a5af 100644 --- a/test.nim +++ b/test.nim @@ -186,6 +186,19 @@ assert(ClassWith3Init.init(5, 5, 5, e=4.5, d=3.4).v1 == 15) +test "Constructor ordering" + +class ClassConstr1: + var v1 = 2 + method makeCopy(): ClassConstr1 = + let n = ClassConstr1.init() + return n + method init() = discard + +assert(ClassConstr1.init().makeCopy().v1 == 2) + + + test "Factory method" class ClassWithFactory: