Re: Reference to sequence

2020-05-10 Thread Stefan_Salewski
> 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?

Re: Reference to sequence

2020-05-10 Thread dataPulverizer
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.

Re: Reference to sequence

2020-05-10 Thread mratsim
@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

Re: Reference to sequence

2020-05-10 Thread dawkot

Re: Reference to sequence

2020-05-10 Thread dataPulverizer
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] ?

Re: Reference to sequence

2020-05-10 Thread dataPulverizer
Many thanks, dereferencing ref s is done using x[] as in your answer.

Re: Reference to sequence

2020-05-10 Thread b3liever
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

Reference to sequence

2020-05-10 Thread dataPulverizer
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