• Post Reply Bookmark Topic Watch Topic
  • New Topic

is it possible to return value from method enclosed in the lambda expression?  RSS feed

 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if yes then which return statement execute ?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37395
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code doesn't compile (which is probably the answer to your question.) Why are you trying to declare a method inside a lambda?
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Your code doesn't compile (which is probably the answer to your question.) Why are you trying to declare a method inside a lambda?


That was dummy code.Just i want to know whether is it possible to return method enclosed in lambda expression
 
Campbell Ritchie
Marshal
Posts: 56209
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the code doesn't compile, doesn't that answer both your questions?
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If the code doesn't compile, doesn't that answer both your questions?


Well... no.  It would answer the question if the OP asked "Is this how you...".  However, the question was "Is it possible to...", which has an implied addendum of "even if this I don't have the correct syntax yet."
 
Campbell Ritchie
Marshal
Posts: 56209
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. As long as we aren't straying into the realms of false ⇒ anything
 
Junilu Lacar
Sheriff
Posts: 11434
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't define a method like that inside a lambda expression body. However, you can return the result of a method call as long as that method is in scope at the point where the lambda is declared.

The JLS defines a LambdaBody as being an Expression or a Block. Since a "Block" can be just about anything that's enclosed in a pair of braces {...} that means there's a lot of leeway in defining a multi-statement lambda body, including having a class declaration in there. Since a method declaration is not valid inside a block but valid inside a class declaration, you can technically have a method declaration inside a lambda body, just not directly in the lambda body but rather as part of a class declaration that's in the lambda body. I hope that makes sense.

You don't want to make your lambda bodies too complicated though because you can easily make a big, unwieldy mess that way. If the lambda is to be treated as a functional interface that returns a value, your lambda body must have at least one return statement in it. Whatever is in that return statement is what your lambda would return.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!