Hi all,
I'm needing help with a seq of tuples of procs, used with this example:
type
Test* = ref TestInternal
TestInternal* = object
o*: seq[ffloat]
var testSeq = @["abc","123"]
proc addTest* (): Test =
# Test constructor.
result = Test(o: newSeq[ffloat]())
result.o.add(0.0);
proc ptest(test: Test): bool {.procvar, discardable.} =
echo "success from ptest"
echo "testSeq = ", testSeq.repr
result = true
var testing123 = addTest()
Now I need a sequence of tuples, with the "myproc" tuple element pointing to
the ptest() procedure, but get a type mismatch error:
var seq_tuple_test = newSeq[tuple[name:string; procID:int; procBody:string;
myproc:proc (test:Test):bool]]()
Error: type mismatch: got (seq[tuple[name: string, procID: int, procBody:
string, myproc: proc (test: Test): bool{.closure.}]], (string, int, string,
proc (test: Test): bool{.gcsafe, locks: 0.}))
but expected one of:
proc add(x: var string; y: cstring)
proc add(x: var string; y: string)
proc add[T](x: var seq[T]; y: T)
proc add[T](x: var seq[T]; y: openArray[T])
proc add(x: var string; y: char)
proc add[A, B](t: var OrderedTable[A, B]; key: A; val: B)
proc add[A, B](t: OrderedTableRef[A, B]; key: A; val: B)
proc add[A, B](t: TableRef[A, B]; key: A; val: B)
proc add[A, B](t: var Table[A, B]; key: A; val: B)
proc add(father, child: NimNode): NimNode
proc add(father: NimNode; children: varargs[NimNode]): NimNode
But this works:
# Create seq_test.
var seq_test = newSeq[tuple[name:string; procID:int; procBody:string;
myproc:proc (test:Test):bool{.gcsafe, locks: 0.}]]()
# Populate the seq, and call our procedure referenced in the tuple.
seq_test.add(("yo",1,"hey",ptest))
discard seq_test[0][3](testing123)
Couple questions:
1) Is there a way for the compiler to not infer 'gcsafe' and 'locks'? I don't
want to hang myself later. For instance, ptest() accesses a global seq of
strings called **testSeq**, and I don't know if that's safe.
2) Can I set the discardable pragma when defining seq_tuple_test? To avoid an
error, I currently must call ptest() like this: **discard
seq_test[0][3](testing123)**