Skip to content

Commit

Permalink
Attempt to fix error in singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
jjv360 committed Dec 26, 2021
1 parent f8a8d8b commit 1148bd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 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.4"
version = "0.2.5"
author = "jjv360"
description = "Adds class support to Nim."
license = "MIT"
Expand Down
31 changes: 20 additions & 11 deletions src/classes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ proc createClassStructure(head: NimNode, body: NimNode, result: NimNode, isSingl
# echo node.treeRepr
# quit()

# If singleton, add the shared function header now
if isSingleton:
let sharedIdent = ident"shared"
let sharedNoVarName = ident"_"
result.add(quote do:
proc `sharedIdent`*(`sharedNoVarName`: type[`className`]): `className`
)

# Gather all mixins
for idx, node in body:

Expand Down Expand Up @@ -396,17 +404,6 @@ proc createClassStructure(head: NimNode, body: NimNode, result: NimNode, isSingl
# Add it
if showDebugInfo: echo "Adding declaration for " & (if isStatic: "static " else: "") & "method: name=" & $methodNode.name & " args=" & $(methodNode.params.len()-2)
result.add(methodNode)


# If singleton, add the shared function now
if isSingleton:
let sharedVarName = ident("shared__" & $className)
result.add(quote do:
var `sharedVarName`: `className` = nil
proc shared*(_: type[`className`]): `className` =
if `sharedVarName` == nil: `sharedVarName` = `className`.init()
return `sharedVarName`
)



Expand Down Expand Up @@ -732,6 +729,18 @@ proc createClassStructure(head: NimNode, body: NimNode, result: NimNode, isSingl
export `m`
)

# If singleton, add the shared function implementation now
if isSingleton:
let sharedIdent = ident"shared"
let sharedVarName = ident("internalShared" & $className)
let sharedNoVarName = ident"_"
result.add(quote do:
var `sharedVarName`: `className` = nil
proc `sharedIdent`*(`sharedNoVarName`: type[`className`]): `className` =
if `sharedVarName` == nil: `sharedVarName` = `className`.init()
return `sharedVarName`
)

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

Expand Down

0 comments on commit 1148bd4

Please sign in to comment.