diff options
author | Zahary Karadjov <zahary@gmail.com> | 2012-04-10 20:53:44 +0300 |
---|---|---|
committer | Zahary Karadjov <zahary@gmail.com> | 2012-04-10 20:53:44 +0300 |
commit | a64f03230afa69f2143b0eb159c9294998c5e19b (patch) | |
tree | 5abc9f9ae2faf8fbbf1462a06fbeb3688e38b254 /tests/run | |
parent | e941a147670442e706d1777f4a1bc368d0cbcdba (diff) | |
download | Nim-a64f03230afa69f2143b0eb159c9294998c5e19b.tar.gz |
proper order of initialization for .global. variables
Diffstat (limited to 'tests/run')
-rw-r--r-- | tests/run/globalaux.nim | 15 | ||||
-rw-r--r-- | tests/run/globalaux2.nim | 4 | ||||
-rw-r--r-- | tests/run/tglobal.nim | 16 |
3 files changed, 35 insertions, 0 deletions
diff --git a/tests/run/globalaux.nim b/tests/run/globalaux.nim new file mode 100644 index 000000000..5f6f72721 --- /dev/null +++ b/tests/run/globalaux.nim @@ -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 + diff --git a/tests/run/globalaux2.nim b/tests/run/globalaux2.nim new file mode 100644 index 000000000..6c77f1f48 --- /dev/null +++ b/tests/run/globalaux2.nim @@ -0,0 +1,4 @@ +import globalaux + +echo "in globalaux2: ", globalInstance[int]().val + diff --git a/tests/run/tglobal.nim b/tests/run/tglobal.nim new file mode 100644 index 000000000..84c4510c1 --- /dev/null +++ b/tests/run/tglobal.nim @@ -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 + |