Skip to content

Commit

Permalink
Fix constructor ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
jjv360 committed Jan 26, 2023
1 parent 72423fa commit 1dae01b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 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.13"
version = "0.2.14"
author = "jjv360"
description = "Adds class support to Nim."
license = "MIT"
Expand Down
19 changes: 9 additions & 10 deletions src/classes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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



Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1dae01b

Please sign in to comment.