I think I have still not really learned that. I think I may write something like
type
ExtSeq = object
s: seq[int]
timestamp: string
var
es: ExtSeq
# init
es.s.add(7)
echo es.s[0]
echo es.timestamp
But I would like to just write
es.add(7)
echo es[0]
Of course I have ideas how to do it. I may define customs procs or maybe templates. But I am not sure what is the best solution or where I can find an example...
You will need to add this proc for the add:
proc add*(es: var ExtSeq; x: int) {.inline.}=
es.s.add(x)
The inline pragma isn't necessary, but it speeds things up a little.
For using the index tokens ([ and ]), you just need to override that proc too:
# Get
proc `[]`*(es: var ExtSeq; index: int) {.inline.}=
return es.s[index]
# Set
proc `[]=`*(es: var ExtSeq; index: int; value: int) {.inline.}=
es.s[index] = value
Thanks for that example!
I think I have seen code somewhere where someone has used templates for something similar, but i can not find it currently. But I also think that inline procs should be fine. Well, for inline procs to work over module boundaries we may need link time optimization enabled in GCC. Guess that was what Jehan told us.
not much tested this on my own, but I think this should make all iterators available
converter toseq[T](arg: ExtSeq[T]): seq[T] =
arg.s
converter toseq[T](arg: var ExtSeq[T]): var seq[T] =
arg.s