It happened multiple times that I wanted to have an object behave as if it were on the stack, however its size caused the stack to overflow.
I know that I can create the object as ref but I don't like the pointer semantics, especially for function arguments. I know that I can de-reference the ref object and pass that into functions, but that creates problems, when this (possibly generic) function tries to copy that object (→ stack overflow).
What I've been doing instead was using seqs with a single element. This way I get exactly the behaviour I want, except that I need to manually initialize it (setLen 1) and that I have to access the element with [0].
# this crashes the stack
var hugeArray: array[largeNumber, float32]
# works but has pointer semantics and can't be used for const vars
var hugeArray2: ref array[largeNumber, float32] = new array[largeNumber, float32]
# my workaround, need to write `hugeArray3[0]` to actually access the element, also need to initialize it with setLen
var hugeArray3: seq[array[largeNumber, float32]]
hugeArray3.setLen 1
# what I would like to do (`hugeArray4` should be otherwise usable exactly like `hugeArray`)
var hugeArray4: HeapAllocated[array[largeNumber, float32]]
Is there a way to write such a HeapAllocated construct? I don't know how the automatic initialization of such an object would work. Or in case that more people would benefit from this, would it make sense to add this to the language?
Or in case that more people would benefit from this, would it make sense to add this to the language?
The hardest part of this would be how to name a "heap value semantics object"