Local Functional Interfaces
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Wed Nov 13 06:29:46 PST 2013
In better words, interface declarations are implicitly static and you can
not have static declarations in local context (method body).
On Wed, Nov 13, 2013 at 1:07 PM, Remi Forax <forax at univ-mlv.fr> wrote:
> 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");
> runner.run();
> }
> }
>
> This seriously reduce the possible interesting use classes making a
> local interface not very useful.
>
> cheers,
> Rémi
>
> 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