Obviously the following is wrong:
proc testCallback[T](callback: proc(this: T) {.closure, gcsafe, locks: 0.}) = callback()
type TestType = object
discard
proc helloNim(this: TestType) = echo "Hello, Nim!"
proc testProc(this: TestType) =
testCallback(this.helloNim) #Want to callback
Must be nested?
type TestType = object
discard
proc helloNim(this: TestType) = echo "Hello, Nim!"
proc testProc(this: TestType) =
var hm = proc () = this.helloNim()
testCallback(hm)
var t: TestType
t.testProc()
some cumbersomeYou can pass a procedure without making anonymous Procs if procedure type match.
type TestType = object
proc testCallback(callback: proc(this: TestType) {.closure, gcsafe, locks: 0.}, this: TestType) = this.callback()
proc helloNim(this: TestType) = echo "Hello, Nim!"
proc testProc(this: TestType) =
testCallback(helloNim, this)
var t: TestType
t.testProc()
or
proc testCallback(callback: proc() {.closure, gcsafe, locks: 0.}) = callback()
proc helloNim() = echo "Hello, Nim!"
testCallback(helloNim)