proper order of initialization for .global. variables
This commit is contained in:
parent
e941a14767
commit
a64f03230a
7 changed files with 62 additions and 3 deletions
15
tests/run/globalaux.nim
Normal file
15
tests/run/globalaux.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
type
|
||||
TObj*[T] = object
|
||||
val*: T
|
||||
|
||||
var
|
||||
totalGlobals* = 0
|
||||
|
||||
proc makeObj[T](x: T): TObj[T] =
|
||||
totalGlobals += 1
|
||||
result.val = x
|
||||
|
||||
proc globalInstance*[T]: var TObj[T] =
|
||||
var g {.global.} = when T is int: makeObj(10) else: makeObj("hello")
|
||||
result = g
|
||||
|
||||
4
tests/run/globalaux2.nim
Normal file
4
tests/run/globalaux2.nim
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import globalaux
|
||||
|
||||
echo "in globalaux2: ", globalInstance[int]().val
|
||||
|
||||
16
tests/run/tglobal.nim
Normal file
16
tests/run/tglobal.nim
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
discard """
|
||||
file: "toop1.nim"
|
||||
output: "in globalaux2: 10\ntotal globals: 2\nint value: 100\nstring value: second"
|
||||
"""
|
||||
|
||||
import globalaux, globalaux2
|
||||
|
||||
echo "total globals: ", totalGlobals
|
||||
|
||||
globalInstance[int]().val = 100
|
||||
echo "int value: ", globalInstance[int]().val
|
||||
|
||||
globalInstance[string]().val = "first"
|
||||
globalInstance[string]().val = "second"
|
||||
echo "string value: ", globalInstance[string]().val
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue