I have a template like this:
template ARR(v:Val):seq[Val] =
cast[seq[Val]](bitand(v,bitnot(UNMASK)))
however, I want to be able to change the result.... like :
a = ARR(x)
a[0] = ....
How can I return a var seq[Val] (or sth like that) ?
var a: seq[Val]
a = ARR(x)
a[0] = ....
But this makes a copy of the sequence, doesn't it?
Is there any way to do the same thing, using the reference? (after all, it's a pointer, if I'm not mistaken... all I want to do is change the value/sequence to which that pointer is pointing)
Pass the sequence to shallow() before processing or create a custom object
type MyShallowSeq[T] {.shallow.} = object
buffer: seq[T]
Your solution will create 2 pointer indirections, it's idiomatic but slower.
What would you think of using... ?:
type
SeqRef[T] = ref seq[T]
proc newSeq[T](s:seq[T]): SeqRef[T] =
new(result)
result[] = s