Constructing a java lambda expression?

Vibhu Mohindra vibhu.mohindra at gmail.com
Thu Aug 25 14:18:44 UTC 2022


Hi Alan,

On 25/08/2022 06:36, Alan Ruttenberg wrote:
> So in order to implement this I need to know the interface to use for 
> the lambda? Presumably I can determine this with reflection.

You learn this not by reflection but by reading the library's source 
code, which you've done below to determine that the interface is 
Consumer not Function.

> An example of one of the functions that takes the lambda is defined:
> 
> public final NitfSegmentsFlow forEachImageSegment(final 
> Consumer<ImageSegment> consumer)
> 
> But consumer has two methods: accept and andThen

As Alessio said andThen() has a default implementation in Consumer (just 
as it did in Function).

> Eventually it looks like accept is called on the consumer.

That sounds right.

> So in this 
> case it looks like I need to use jinterface-implementation and define 
> the accept method, no lambda necessary?

You need to implement an accept() method somehow. One way is similar to 
how I'd shown an implementation of java.util.function.Function.apply(). 
(At the time I didn't know what type of object your library wanted so I 
assumed for concreteness that it was Function. Now we know it's 
Consumer. The principle of the technique I showed still applies.)

I showed how to implement an adaptor class in Java. I noted that you 
could probably do the equivalent from ABCL. Alessio pointed out that 
that way was to use jinterface-implementation. So you don't _need_ to 
use a jinterface-implementation approach, but you can.

You're right, it doesn't seem like you need to think about Java lambdas, 
just a Java interface (Consumer) and getting a new class that implements 
that interface and instantiating it to get an object. Whether you write 
that class in Java or by using jinterface-implementation in ABCL is a 
different matter.

> What confuses me now is if a lambda is passed as the test code I'm 
> reading does:
> 
> forEachImageSegment(imageSegment -> {do something})
> 
> How does java know that the lambda is the implementation of accept 
> rather than andThen.

As Alessio described, Java can only know this if an interface has a 
single (abstract) method in it. It then assumes that the lambda 
represents an implementation of that single method. You can see from the 
javadoc for Consumer that though it has more than one method, all but 
accept() have a "default" next to them, which means that the Consumer 
interface already provides a concrete implementation of them. That is, 
they aren't abstract. accept() doesn't have a "default" next to it, so 
it's abstract, meaning all concrete classes that claim to implement the 
interface must provide a concrete implementation of (at least) accept().

> Or more practically, how do I figure out which 
> method to implement without reading the source code?

1. By reading the javadoc for Consumer. I showed you an example for 
Function, which specifies a number of methods but only one (apply()) was 
abstract.
https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html

2. By asking on this list apparently :-)  The answer is accept().

----
If you want to take a small step first, modify what I sent you earlier 
to work with Consumer instead of Function. The modification should be 
trivial. Once you've got that working, and if you don't like that the 
adaptor is written in Java or like experimenting, then explore the 
jinterface-implementation approach to do everything directly from ABCL.


-- 
Vibhu



More information about the armedbear-devel mailing list