Is there a way to have references of tuples, i.e. to have dynmically allocated tuples? Could the following code be made working?
type
ObjectRef = ref object
oname: string
otype: int
TupleRef = ref tuple
tname: string
ttype: int
var
ods = newSeq[ObjectRef]()
tds = newSeq[TupleRef]()
ods.add(ObjectRef(oname: "Some object", otype: 4))
tds.add(???(tname: "Some tuple", ttype: 5))
Yes, with a helper function:
proc box[T](arg: T): ref T {.inline.} =
new result
result[] = arg
tds.add(box((tname: "foo", ttype: 0)))
What about this?
type
TupleRef = ref tuple
name: string
proc newTupleRef(name: string): TupleRef =
new(result)
result.name = name
var t = newTupleRef("tupleref")
echo(repr(t))
echo(repr(t[]))
AxBen: "new() creates an object, sth that I wanted to avoid (I'd also like to avoid using unmanaged storage though). Why isn't there anything like newTuple(...)?"
The first thing that newAnything() does is call new(result). Your request doesn't appear to make sense ... a dynamically allocated tuple is a new object, so how can you ask for one without the other? If you don't want to call either new (which is for gc'ed objects) or alloc (which is for non-gc'ed objects), where do you expect the memory for the tuple to come from?
@andrea
Thx, now I've got it too...