Proposal: Match hook and callback type
DavidGoldman opened this issue · 3 comments
Just a suggestion, perhaps you've thought of this and it won't work - I might be missing something.
Since the underlying implementation uses imp_implementationWithBlock
, the hooked function has a different type than the block you supply (notably no selector). Would it be possible to instead pass an IMP directly, and then it might be possible to make sure they both have the same type? (type check that the callAsFunction and the block you supply have the same type)
Example here although I haven't tested it with the calling convention requirements:
func genericFunction<T>(_ selName: String, _ callback: @escaping (String, T.Type) -> T) {
print("hi")
callback("foo", T.self)
}
func noop() {}
genericFunction("test") { (str, type) -> () -> Void in
print("Called with \(str)")
print(type)
return noop
}
This example passes the type into the method but you could potentially capture it into Task
itself/make Task generic.
EDIT: Oh I think I see the issue, it has to be a block if you want to be able to reference the task.
Hey there! Thanks for writing an issue! 👋' first issue
Hey David! I was playing with this, wanted to wrap it but I can't abstract changing calling conventions: https://twitter.com/steipete/status/1266799174563041282?s=21
If you find a way please reopen and let me know. I know there's NSInvocation, but that's a lot of magic for what should be a simple library. https://github.com/mhdhejazi/Dynamic/blob/master/Sources/Dynamic/Invocation.swift