• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

question on lambda expressions and conflicting scope of variables

 
Ranch Hand
Posts: 684
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Venkat Subramaniam
Can you please look at this post I made in the certification forum?
https://coderanch.com/t/775369/certification/Sybex-Ch-Lambdas-difference-compiler

per Jeanne's comment,

In location B, the lambda isn't in scope anymore so "c" doesn't produce such a conflict.



I still do not understand why the variables are in scope in 'LOCATION A', but out of scope in 'LOCATION B'.
To me, a lambda expression is something inserted that exists beyond the life of that code block.
It will be executed sometime later on, outside the life of the block.
It is like an alien visiting earth
So, both locations A and B have to be taken into account.
 
Bartender
Posts: 3958
43
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Lambda expression can use a local variable (if it is final or effectively final) -- it's called a lambda closure

2) The code you're talking about follows Java variable scope rule:

CASE 1:



CASE 2:

 
Ranch Hand
Posts: 885
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:@Venkat Subramaniam
Can you please look at this post I made in the certification forum?
https://coderanch.com/t/775369/certification/Sybex-Ch-Lambdas-difference-compiler

per Jeanne's comment,

In location B, the lambda isn't in scope anymore so "c" doesn't produce such a conflict.



I still do not understand why the variables are in scope in 'LOCATION A', but out of scope in 'LOCATION B'.
To me, a lambda expression is something inserted that exists beyond the life of that code block.
It will be executed sometime later on, outside the life of the block.
It is like an alien visiting earth
So, both locations A and B have to be taken into account.



I think this answers your question.
Here's the code you're asking about:


And here's the code to be inserted:


If you insert the code at location A then variables "start" and "c" are already declared which causes problems when they're re-declared in the lambda. However, at location B, the variables declared inside the lambda are "out of scope" because they're declared inside the lambda and the closing curly bracket for the lambda occurs before we get to location B.

Hoping this helps,
Burk
 
Anil Philip
Ranch Hand
Posts: 684
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the code example. It made things clearer.

Mikalai Zaikin wrote:1) Lambda expression can use a local variable (if it is final or effectively final) -- it's called a lambda closure

2) The code you're talking about follows Java variable scope rule:

CASE 1:



CASE 2:

 
Anil Philip
Ranch Hand
Posts: 684
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't Lambdas use scope more like Local Classes rather than block scope or what you refer to: "Java variable scope rule"?
(Wouldn't scope rules for local classes be more appropriate).

Mikalai Zaikin wrote:1) Lambda expression can use a local variable (if it is final or effectively final) -- it's called a lambda closure

2) The code you're talking about follows Java variable scope rule:

 
Bartender
Posts: 15743
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

Lambdas are often used to close over a variable that has been declared outside the lambda expression. If you were able to shadow the outer variable with a lambda parameter, that could lead to a lot of confusion for people reading your code.

The same is probably true for local classes, but I can think of two reasons why this restriction doesn't apply to them:

  • The compiler reuses most of the code for parsing normal classes to parse local classes, and normal classes don't have such a restriction.
  • Local classes were added to the language waaay back in Java 1.1, and the designers may not have had the foresight to add the restriction.

  • With lambda expressions, the designers had a new opportunity to add this restriction to them.
     
    There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
    Clean our rivers and oceans from home
    https://www.kickstarter.com/projects/paulwheaton/willow-feeders
    reply
      Bookmark Topic Watch Topic
    • New Topic