Hello there,
In tracking a bug in our codebase I have bumped into some funny behavior with sink parameters while using refc (nim 1.16.18 and 2.0.2 on Linux), and was wondering if I am missing anything silly. This example captures the issue:
type AnObject* = object of RootObj
value*: int
proc mutate(a: sink AnObject) =
a.value = 1
var obj = AnObject(value: 42)
echo "Value is: ", obj.value # Value is: 42
mutate(obj)
echo "Value is: ", obj.value # Value is: 1
From the manual, I understand that the compiler should make a copy of obj as there's a read to it after the call to mutate, but that does not seem to be happening. If I instead declare:
type AnObject* = object
value*: int
then it works as expected. Am I making the wrong assumption here?
Thanks in advance.
I think the reason is that sink with refc doesnt do anything, because ownership semantics only work with A/ORC.
AnObject in first example not copied because big enough object automatically passed by reference (optimization by compiler)