I have two pieces of code that I expect to produce the same result but they don't.
import tables, times
var t = initTable[int, seq[Duration]]()
t.add(1, @[])
t[1].add(Duration())
echo t
Result: {1: @[0 nanoseconds]}
import tables, times
var t = initTable[int, seq[Duration]]()
t.add(1, @[])
var temp = t[1]
temp.add(Duration())
echo t
Result: {1: @[]}
Can someone explain why storing a result in a temp variable can change the result like this?
@filip for future reference, you can do something like this:
import tables, times
type
Storage = ref object # need this in order to store the reference and not make a copy
durs: seq[Duration]
proc `$`(st: Storage): string =
return $st.durs
var t = newTable[int, Storage]()
t.add(1, Storage(durs: @[]))
var temp = t[1]
temp.durs.add(Duration())
echo t
echo temp
# {1: @[0 nanoseconds]}
# @[0 nanoseconds]