• 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

No FlatMappable Interface?

 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the problems I've run into is trying to write generic code for Java 8. It would seem reasonable to be able to write a function that can accept either an Optional or a Stream and perform a flatMap on that argument... but I can't convince generics to let me do this and there's no common interface for those types. That same problem exists for filter and map (and probably others).

Questions to Richard:
  • Do you feel the absence of these common interfaces is a mistake?
  • Do you have any recommendations for writing truly generic code in the absence of such interfaces?

  •  
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't really see what generics have to do with it.

    If I understand your problem correctly, I would say that if there was a mistake, it was that Optional does not extend Stream.
     
    Sean Corfield
    Rancher
    Posts: 379
    22
    Mac OS X Monad Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Optional should not extend Stream - that makes no sense to me - but they do have common functionality (flatMp(), fliter(), map(), etc) but the signatures are different for flatMap(). My comment was about writing generic code (not "generics" code), although I feel that Java's generics aren't powerful enough to allow me to write this code, even if there was a common interface. Check the signatures - hopefully you'll understand what I mean.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15510
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why not? You can consider an Optional to be a Stream with either zero or one element.

    Anyway, can you give an example of a scenario where you would want to write the code you described? I'm not quite sure what you'd like to achieve.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic