> would be a builtin for transforming something to ref,
Have you been able to read
[http://ssalewski.de/nimprogramming.html#_references_and_pointers](http://ssalewski.de/nimprogramming.html#_references_and_pointers)
If so, what may I improve?
Thanks, I don't know why I didn't think of that. I thought there would be a
builtin for transforming something to ref, but this is fine.
@b3liever post shows you how to dereference a ref, not a seq
i.e. if it wasn't clear
proc assignToRef[T](x: T): ref T =
new result # allocate the ref object, any type works
result[] = x # assign it x
let y = assignToRef(@[1.0, 2, 3, 4, 5])
Run
Sorry quick extension to that question, if I have a variable that is a seq[T],
how do I assign it to a new variable that is a ref seq[T] ... or otherwise how
do I transform a variable that is a seq[T] to a ref seq[T] ?
Many thanks, dereferencing ref s is done using x[] as in your answer.
You need to allocate x seq in the heap, or else x might go out of scope and its
data will get deallocated.
var x: ref seq[float64]
new(x)
x[] = @[1.0, 2, 3, 4, 5]
let y = x
Run
if you don't care then use your snippet
How do you create a reference to a sequence? I can see how you create a pointer
to a sequence:
var x: seq[float64] = @[1.0, 2, 3, 4, 5];
var y = x.addr;
Run
But have no idea how to create ref seq[float64], or is it only possible to
create a ptr seq[float] while