Protected variable access inside Lambda blocks
Mark Mahieu
markmahieu at gmail.com
Sat Nov 13 12:22:59 PST 2010
Hi,
I think that's because lambda expressions are now lexically scoped - see
section 7 in
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-3.html
All you need to do in this case is qualify your reference to 'DEFAULT', ie.
use 'LambdaAccess.DEFAULT' instead.
Regards,
Mark
On 13 November 2010 19:28, Arul Dhesiaseelan <aruld at acm.org> wrote:
> Hi,
>
> I am trying to access a protected final defined in my SAM type from within
> a
> Lambda block, but it fails with "cannot find symbol" compile error with the
> prototype.
>
> Here is a simple test:
>
> public class ProtectedAccess {
> public static abstract class LambdaAccess<T> {
> protected static final String DEFAULT = "DEFAULT";
> public abstract T block();
> }
>
> public static void main(String... args) throws Exception {
> LambdaAccess<String> lambdaAccess = #{
> return DEFAULT;//compile error: unable to access this protected
> constant
> };
> lambdaAccess.block();
>
> LambdaAccess<String> anonymousAccess = new LambdaAccess<String>() {
> public String block () {
> return DEFAULT;//works just fine in case of anonymous types
> }
> };
> anonymousAccess.block();
> }
>
> }
>
> I am not sure if this is a limitation or if I am doing something wrong
> here.
>
> -Arul
>
>
More information about the lambda-dev
mailing list