• 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:

Sybex 829 Ch 8 Lambdas:Why does it make a difference to the compiler where these lines are inserted?

 
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
tb864585.JaSE17SG.c08.10

Boyarsky-Selikoff chapter 8 appendix




When these lines are put in LOCATION A, there is a compiler error:




But when the same lines are moved to LOCATION B, there is no error.

Why does it make a difference to the compiler where these lines are inserted?


 
Sheriff
Posts: 28436
104
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler error (or anyway, one of the two) I get in Eclipse is this:

Lambda expression's local variable start cannot redeclare another local variable defined in an enclosing scope.



That's a bit more explanatory than the messages you got. Why you can have a local variable redeclaring a local variable defined in a lambda expression (as per your Location B) but not vice versa (Location A) is a mystery to me but there it is.

Hope that helps.
 
author & internet detective
Posts: 42173
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The lambda only cares about things that come before it. When the code is in location A and the lambda comes along, "c" is taken and can't be reused. In location B, the lambda isn't in scope anymore so "c" doesn't produce such a conflict.
 
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

Jeanne Boyarsky wrote:The lambda only cares about things that come before it. When the code is in location A and the lambda comes along, "c" is taken and can't be reused. In location B, the lambda isn't in scope anymore so "c" doesn't produce such a conflict.



I thought the scope for Lambdas would be similar to anonymous classes since it is implemented as such?
 
Paul Clapham
Sheriff
Posts: 28436
104
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:The lambda only cares about things that come before it. When the code is in location A and the lambda comes along, "c" is taken and can't be reused. In location B, the lambda isn't in scope anymore so "c" doesn't produce such a conflict.



This is also true if you replace the lambda with an ordinary { ... } code block, so evidently the rule has been around since the beginning of Java. That was something I didn't know because I wouldn't ever have written code with duplicate local variable names like that.
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic