Local Functional Interfaces
Peter Levart
peter.levart at gmail.com
Wed Nov 13 23:09:34 PST 2013
On 11/13/2013 03:29 PM, Ali Ebrahimi wrote:
> In better words, interface declarations are implicitly static and you can
> not have static declarations in local context (method body).
Right, and this (currently) applies to static methods too. I.e. the
following:
static void staticMethod() {
int x = 1;
class LocalClass { // works!
void m() {
System.out.println(x);
}
}
interface LocalInterface { // 'interface' not allowed here
default void m() {
System.out.println(x);
}
}
}
...references to local variables from local classes are implemented as
fields in local classes, where the values of local variables are
captured - which implicitly requires state - and interfaces have no state...
But a hypothetical "static local" class or interface would be possible -
with rules that prevent its methods from accessing local or instance
variables/methods of enclosing scopes. There could even be a consistent
syntax for such things:
static void staticMethod() {
int x = 1;
static class LocalClass {
void m() {
System.out.println(x); // can't access local var
}
}
interface LocalInterface { // 'interface' is implicitly static
default void m() {
System.out.println(x); // can't access local var
}
}
}
Consistent in sense that Java already has a "static class" in the form
of nested class - with rules that prevent its methods from accessing
instance variables/methods of enclosing scopes...
All that is possible, but the question is whether it is desirable. Where
would it be usable? Would you want to have a local interface with a name
and particular signature in one method, and a local interface with same
name and different signature in some other method of the same class?
Would it discourage interface re-use? Java already has a "private
interface" which prevents it's visibility outside the declaring class.
This is, in my opinion, enough hiding for a feature that was meant to
define a contract...
Regards, Peter
>
>
> 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