I'd like to create two objects hold a ref to the same Matrix, what am I missing?
Many thanks for a hint!
type
Matrix[T] = ref object
data : seq[T]
MX[T] = object
Theta : ref Matrix[T]
MXU[T] = object
Theta : ref Matrix[T]
var
M : MX[float]
MU : MXU[float]
M.Theta= new Matrix[float]
#[ gives type mismatch: got 'Matrix[system.float]' for 'new Matrix[float]'
but expected 'ref Matrix[system.float]'
]#
M.Theta.data= new seq[float]
M.Theta.data = @[1.0,2.0]
MU.Theta = M.Theta # do both point to the same matrix?
MU.Theta.data[0]=7.0
echo M # I'd like to see [7.0,2.0]
A side comment... I find using new Xxx extremely confusing, error prone and inconvenient, and try to avoid it as much as possible.
The Xxx() constructor feels much better, and it would do implicit new call. And you don't need to change the code if later you would decide to change the object from ref to plain object.