I have been experimenting with Nim for about a day now and I was wondering how you can inherit a type from the built-in ( seq in particular) so that procedures running on seq can also process a custom type.
I included a minimal example below in which TestCol wraps / proxies the sequence - will there be a way to have TestCol support map , filter , etc. without redefining procedures?
type TestCol*[T] = object data*: seq[T] proc len*(b: TestCol): int = b.data.len proc `[]`*[T](b: TestCol[T], idx: int): T = b.data[idx] proc `[]=`*[T](b: var TestCol[T], idx: int, item: T) = b.data[idx] = item var x = newSeq[int](3) var y = TestCol[int](data: x) y[0] = 1 y[1] = 2 y[2] = 3 for n in map(y, proc (x: int): int = x + 1): echo($n)
It is preferable that the solution does not require converting the user sequence to a regular sequence for performance reasons, while the conversions are less trivial than higher (although what Iβll do now, as suggested)
A use case in the real world is to implement array helpers on RingBuffer.nim
source share