Hi, I had a bug in some of my nim code and it produced some unusual behaviour. It seems that the nim compiler assigns the result of a function before the said function has completed. I managed to reproduce it:
import tables
var values:Table[string,string]
values["item 1"] = "this is item 1"
values["item 2"] = "this is item 2"
proc addItemToValues():Table[string,string] =
echo "values variable within function:"
echo values
values["item 3"] = "this is item 3"
echo "values variable before function:"
echo values
values = addItemToValues()
echo "values variable after function:"
echo values
produces:
values variable before function:
{"item 1": "this is item 1", "item 2": "this is item 2"}
values variable within function:
{:}
values variable after function:
{"item 3": "this is item 3"}
I wouldn't consider this a 'bug' in the compiler, because the code is written incorrectly, but I am wondering if that is the intended behavior?
I seem to remember reading about this behavior somewhere, but for the life of me I cant' find it.
It's not clear.
https://nim-lang.org/docs/manual_experimental.html#aliasing-restrictions-in-parameter-passing
An output parameter should never be aliased with a global or thread local variable referenced by the called proc.
Arguably the compiler needs to detect and reject your code. Realistically it won't do that anytime soon and you need to write cleaner code.
I totally agree. Thanks for your input.