Replies: 2 comments 5 replies
-
Thank you Alexis! |
Beta Was this translation helpful? Give feedback.
-
I've been working on building lambda support using extern types first, to get the mechanics of everything all working, then transitioning to add support directly in the generator. Here is where I'm at. Need: Proposal: Syntax: inline parameter syntax Syntax examples:
Implementation: For ObjC the implementation is fairly simple, as toCpp just needs to wrap the block into a std::function, and fromCpp just needs to make a block to wrap the std::function. Java is more complex. It would appear on the java side just as we can do now, generating an interface with one method. It would still need the full proxy object, the toCpp would be able to wrap the proxied object with a lambda and store in the std::function. fromCpp would need to create an implementation of the c++ interface that delegates to the std::function and is returned to java via the normal interface proxy. This creates two downsides:
One approach would be to use generics, thus you could make N permutations where N is the max number of args +1, The downside to this approach is that all types need to be boxed. Another approach would be to create the functional interface names using the same algorithm as the names for Jni functions, this would yield consistent names for the interfaces, but means the interface name would change if the signature changed, potentially causing issues if the interfaces are formally 'implemented' on the java side, as opposed to using the lambda syntax. In order to defer this hurdle, I've opted to only implement the formal type syntax initially. |
Beta Was this translation helpful? Give feedback.
-
I made a draft PR which added support for c++ lambdas reminiscent to how Java/Kotlin does for Single Abstract Method "functional interfaces".
In general, when I'm calling into Java or ObjC and passing a callback or listener, it's with a lambda that I'd like to do this - and since Djinni doesn't support this natively, this results in the use of utility wrapping classes which implement the functional interface that djinni requires. If one is conscientious in naming the functional interface's one method, c++ templates can make it easier to instantiate these wrappers/adapters, but it's still IMO a bit annoying when all you really want is a lambda in the first place.
Anyway, I'm closing that draft PR for now, but would be interested in keeping the lambda discussion alive in the hope that some solution will eventually make its way into Djinni. @eakoli had mentioned in this post work being done on adding full lambda support - which would clearly be great.
Beta Was this translation helpful? Give feedback.
All reactions