Local Functional Interfaces

Remi Forax forax at univ-mlv.fr
Wed Nov 13 01:37:58 PST 2013

Hi Olena,
interface local to method (like enum BTW) are not valid in Java,
so it's not really an issue related to lambdas but more an issue related 
to allowing local interface in method.

First, because the interface defined a static context, local interface 
can be only defined in a static method,
otherwise you can have an initializer or a default method in the 
interface that access to the field
of the enclosing class.

public class Snippet {
   static void myMethod() {
     interface Runner { public void run(); }
     Runner runner = () -> System.out.println("hello");

This seriously reduce the possible interesting use classes making a 
local interface not very useful.


On 09/23/2013 09:27 PM, Olena Syrota wrote:
> Dear lambda dev team,
> Recently I made experiments with lamdas and found exotic case.
> I was not able to declare local interface to make a "local" lambda-function.
> Why I needed this.
> Suppose I was to implement "local" lamdba-function to be used only couple
> of times inside my method.
> Compiler did not allowed me to place interface inside function.
> In my case it was interface Dropper to implement lambda-function that drops
> last element.
> void myMethod(...) {
> Interface Dropper { dropLastSymbolInChain (List<List<String>> chain);}
> Dropper d = x -> x.stream()
> .map(e->e.subList(0, e.size()-2))
> .collect(Collectors.toList());
> List<List<String>> chain1Dropped = d.dropLastSymbolInChain(chain1);
> List<List<String>> chain2Dropped = d.dropLastSymbolInChain(chain2);
> }
> I was forced to declare interface Dropper outside myMethod.
> Should the proposition on local interfaces be submitted to jep?
> Thank you.
> Olena

More information about the lambda-dev mailing list