Skip to content

Instantly share code, notes, and snippets.

@cryzed

cryzed/test.nim Secret

Created December 7, 2015 15:42
Show Gist options
  • Save cryzed/3a11d40be667d2444b7a to your computer and use it in GitHub Desktop.
Save cryzed/3a11d40be667d2444b7a to your computer and use it in GitHub Desktop.
type
RefNode = ref object
children: ref seq[string]
Node = ref object
children: seq[string]
proc main() =
#var a = RefNode(children: new seq[string])
var a = RefNode()
echo("a:", a.repr) # a:ref 00578028 --> [children = nil]
# Doesn't allocate a the seq[string] object, just a pointer pointing to nil
new(a.children)
echo "a:", a.repr # a:ref 00328028 --> [children = ref 00328038 --> nil]
# a.children.add("Hello World")
# echo "a:", a.repr
var b = Node(children: @[])
echo "b:", b.repr # b:ref 00438048 --> [children = 00438058[]]
b.children.add("Hello World")
# Change in object address. Was the previous "children" object copied
# modified and then reassigned?
echo "b:", b.repr # b:ref 00578048 --> [children = 0057c368[0057c3c8"Hello World"]]
when isMainModule:
main()
@dom96
Copy link

dom96 commented Dec 7, 2015

Here is the output I get:

~/temp_code » nim c -r foo
Hint: system [Processing]
Hint: foo [Processing]
Hint:  [Link]
Hint: operation successful (9891 lines compiled; 0.217 sec total; 14.148MB; Debug Build) [SuccessX]
a:ref 0x10ae23050 --> [children = nil]

a:ref 0x10ae23050 --> [children = ref 0x10ae23068 --> nil]

b:ref 0x10ae23080 --> [children = 0x10ae24050[]]

b:ref 0x10ae23080 --> [children = 0x10ae2c050[0x10ae28530"Hello World"]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment