I am very new to using Nim, so this might be an obvious question.
I have a vector type which I cast as an array. The issue is that I need the inputs to be calculated, so they are not known at compile time. I know that sequences are the way to go, but I would like to keep the length of the list immutable. Is their any way to achieve this?
type Vector*[X: static int] = array[X, int]
var vec: Vector[10] = someInputs()
Thank you so much!
Someone really needs to make a FixedSeq[T] type, but anyway the way to do this is roughly:
import std/typetraits
type FixedSeq[T] = distinct seq[T]
proc init*[T](_: typedesc[FixedSeq[T]], size: int): FixedSeq[T] = FixedSeq[T](newSeq[T](size))
proc `[]`*[T](fixedSeq: FixedSeq[T], i: int): T = fixedSeq.distinctBase()[i]
proc `[]`*[T](fixedSeq: var FixedSeq[T], i: int): var T = fixedSeq.distinctBase()[i]
proc `[]=`*[T](fixedSeq: var FixedSeq[T], i: int, val: T) =
fixedSeq.distinctBase()[i] = val
iterator items*(fixedSeq: FixedSeq): auto =
for x in fixedSeq.distinctBase.items:
yield x
iterator items*(fixedSeq: var FixedSeq): auto =
for x in fixedSeq.distinctBase.mitems:
yield x