Fork me on GitHub
#beginners
<
2016-10-27
>
shadowmint00:10:23

Anyone here able to shed some wisdom on using java 8 closures from clojure?

shadowmint00:10:00

Specifically I'm using http://grpc.io as an application server, and trying to get away with a thin java layer that calls out to clojure for everything.

shadowmint00:10:35

The question is, how do you call back into the java context once you've marshalled a response object on the clojure side?

shadowmint00:10:16

It'd be extremely convenient to be able to do something like:

shadowmint00:10:37

...but it seems you can't?

seancorfield01:10:05

What you’re being passed is an object that implements a Functional interface.

seancorfield01:10:19

I suspect there’s a .call() method on it?

seancorfield01:10:27

What have you tried @shadowmint ?

shadowmint01:10:50

I've tried doing that I wrote up there:

shadowmint01:10:00

Calling the IFn with a closure on it:

shadowmint01:10:09

handler.invoke(..., () -> { ... })

shadowmint01:10:16

but closures are not object?

shadowmint01:10:31

and the IFn interface for clojure functions is overloaded with any number of object artguments

shadowmint01:10:38

but none of them match for a closure type?

shadowmint01:10:13

...I mean, I can have a custom object, with a method on it called 'callback', pass the object in, and invoke the method on it using (.callback promise response).

shadowmint01:10:23

It's just semantically irritating.

seancorfield16:10:49

@shadowmint Ah, I thought you had the Java side working and just needed to know how to call the closure from Clojure — but I see you can’t even compile the Java code.

seancorfield16:10:57

The problem here is that Java uses the type of the argument to decide how to bundle up the closure as a Java object. IFn just accepts objects, so it doesn’t give the Java compiler enough of a "hint" to be able to deduce that. You might be able to insert a Java cast here to satisfy the compiler but I’m not sure.

seancorfield16:10:53

Maybe java.util.function.Consumer<HelloReply> would be a good thing to try first — and then in your Clojure code, call (.accept callback reply). But you’ll have to read the java.util.function package docs and try a few things I suspect.