Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

No FlatMappable Interface?  RSS feed

 
Sean Corfield
Ranch Hand
Posts: 314
14
Clojure Linux Mac OS X Monad
  • Mark post as helpful
  • send pies
  • 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?

  •  
    Stephan van Hulst
    Saloon Keeper
    Posts: 7690
    139
    • Mark post as helpful
    • send pies
    • 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
    Ranch Hand
    Posts: 314
    14
    Clojure Linux Mac OS X Monad
    • Mark post as helpful
    • send pies
    • 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: 7690
    139
    • Mark post as helpful
    • send pies
    • 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.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!