I can't understand why the final comparison in the code below returns false. XOpts.field is a reference, so passing it somewhere shouldn't change really change what addr returns. At least that's my assumption, but I'm obviously missing something here. I would be grateful if someone could clue me in.
type
XObj* = object
val*: tuple[a, b: string]
XObjRef* = ref XObj
XOpts* = object
field: XObjRef
proc return_xobjref(opts: XOpts): XObjRef =
return opts.field
when is_main_module:
var xobjref: XObjRef
new(xobjref)
xobjref.val = ("a", "b")
var opts: XOpts
opts.field = xobjref
var result = return_xobjref(opts)
echo "IS IT THE SAME? ", (addr xobjref) == (addr result)
Hi. You are comparing the current location of the ref. (pointer to ref)
Try to this (dereferencing ref and get pointer to content)
echo "IS IT THE SAME? ", (addr xobjref[]) == (addr result[])
Or if you want to compare the addresses only:
echo "IS IT THE SAME? ", (cast[int](xobjref) == cast[int](result))
Thanks Parashurama & Varriount, that helps a lot. I know where I made the mistake.
LeuGim: I have == proc defined for XObjRef, so the comparison would return true but I that's not "value equality" I want (my use case: using an object as a sentinel value).