• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to handle not void return type in Lambda Expression

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a functional interface I --



Tried to call this below--


Get error ---
Cannot return a void result

For void return type lambda expression is working fine but if I want to use non void return type what should be the approach.

Thanks
Abhra
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry posted in a hurry , Need to add a return statement only.



Thanks
Abhra
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Abhra,

why not change the interface method into:

Then you do not need to return anything.

But such an interface already exists: see the API of BiConsumer.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhra Kar wrote:Create a functional interface I . . . .

PS could also have told you that you have there replicated this interface. But it is far better to parameterise the interface, so you would declare it as and implemented it as
 
Abhra Kar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys.I implemented as suggested by you.

Regards,
Abhra
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure

Always check the return types of your methods. You often use λs in the context of Streams. A λ evaluating to an int can be used if you want an IntStream; one avaluating to Integer is good if you want a Stream<Integer> and one which is void can be used in a terminal operation.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:(...) and implemented it as


But is can be economical to extend an interface to get rid of all these parameters. For instance

so now you can say

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks almost as easy a way to save work writing actual type parameters as the Laputans' way to save work digging fields.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if it was only for one instance, it would not save any typing or copying. But I sometimes have a generic class with a pretty big type collection, and needing a couple of instance or in an enhanced for-loop. I was happy with my way of doing as described above. It did save on typing and copying. I wish I could apply it to Map.Entry<list of horribly long types>, but unfortunately, that class is abstract, so I could not extend it into something concrete.

But your reference to Gullivers Travels seems to indicate a different experience. Can you elaborate about your experience?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . But your reference to Gullivers Travels seems to indicate a different experience. . . .

No, it doesn't.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I haven't got a clue what you mean, but I don't mind keeping it that way.

The idea a couple of years ago was to try to mimic Scala's 'type A = B' (or C's typedef), but I'm always in for some better suggestions (but not in this topic, it is polluted enough already).
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with extending functional interfaces is that you can't really use them as formal parameter types unless you don't mind losing the option to accept pre-existing or composed functions:

It would have been nice if Java supported type aliases, but since it doesn't I recommend that you keep using the least specific type for your formal types, even if they include lots of generic type variables.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Stephan.

At the time I experimented wit this, I did not have java 8, and so never applied it to interfaces and what consequences this would have. Interesting...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic