Easily declaring java lambdas of cross-sector types

Let's say I have a method similar to the following:

public <T extends Supplier<Collection<String>> & Serializable> void doSomething(T supplier){
    new ArrayList<String>().stream().collect(Collectors.toCollection(supplier));
}

      

Why is this

doSomething(() -> new ArrayList<>());

      

causes a problem, but

doSomething((Supplier<Collection<String>> & Serializable)() -> new ArrayList<>());

      

OK? The lambda itself is the same in both cases, and in the second it works. Does this mean the JVM is capable of creating serializable lambdas, however the compiler cannot recognize that this is possible?

Edit: changed the first call to what I intended.

+3


source to share


1 answer


From JLS 15.16. Exclusive expressions :

The caste can be used for an explicit "tag" lambda expression or a method reference expression with a specific target type. To provide an appropriate degree of flexibility, the target type may be a list of types denoting the type of intersection, if the intersection induces a functional interface (ยง9.8).



and no comments why it is not displayed automatically. My guess: this is possible, but not yet implemented. Also interesting is what Eugene said in the comments. Could this mean that something like this is happening in Java 9?

0


source







All Articles