Sometimes it's handy to assign a value object to variable without copying, as if it's a reference.
Is that possible? Usually it's for modifying nested objects stored inside table. You need to either use long references like table["a"].b.c = new_value or update the table like table["a"] = updated_value, wondering if there's some trick to make it easier?
import tables
type O = object
v: int
var t = { "a": O(v: 1) }.to_table
echo t
t["a"].v = 2
echo t // Table updated
var refv = t["a"] # How to make refv to be same as v?
refv.v = 3
echo t // Table not updated
decls has a byaddr macro which lets you easily do this behaviour when you need to.
import std/[tables, decls]
type O = object
v: int
var t = { "a": O(v: 1) }.to_table
echo t
t["a"].v = 2
echo t # Table updated
var refv {.byaddr.} = t["a"] # How to make refv to be same as v?
refv.v = 3
assert t["a"].v == refv.v
Well if the data you're holding moves whilst you're holding onto a pointer it will have a dangling pointer. As such you mostly want to ensure you do not cause the data pointed at to move whilst using it, this is where a borrow checker comes into play typically, not allowing the place of borrowing to be mutated whilst a borrow exists.
import std/[tables, decls]
type O = object
v: int
var t = newSeqOfCap[O](1)
t.add O(v: 100)
var refv {.byaddr.} = t[0]
t.add O(v: 300)
refv.v = 3
assert t[0].v == refv.v