I just tried to use mgetOrPut() of tables module. I was a bit surprised that modifying the returned result was possible as said in the docs, but the table itself was not modified, So I tested with mget(), because that is similar but simpler:
import tables
type
Y = tuple
i: int
proc main() =
var
t = newTable[string, Y]()
y: Y
y.i = 1
t["one"] = y
echo t["one"].i
t.mget("one").i = 3
echo t["one"].i
var x = t.mget("one")
x.i = 5
echo t["one"].i # one may expect 5, but it is 3
main()
1 3 3
From the behaviour of mitems for arrays iterators one may expect 5 as last result. After some thinking I assume that this is not a bug but intended -- modifying the table entry itself would only work with hidden pointers, which may be dangerous.
I wonder -- mget() is deprecated, mgetOrPut() is not deprecated?
[EDIT]
OK, I am not the first who is surprised:
http://forum.nim-lang.org/t/1477#9246
I still wonder what with this behaviour the use cses for mgetOrPut() are...