-
-
Save cryzed/3a11d40be667d2444b7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the output I get: