4 ms·
actually that would be a bad idea. considering you want to handle multiple options at once. the code would get really messy with flatMap/map another way would b
by merb 11y ago
actually that would be a bad idea.
considering you want to handle multiple options at once.
the code would get really messy with flatMap/map
another way would be (now consider more options and it will be way more ugly or considering a list of optional where every optional needs to be present to calculate something):
if (opt1.isPresent() && opt2.isPresent() && opt3.isPresent()) {
int i1 = opt1.get();
int i2 = opt2.get();
int i3 = opt3.get();
}
of course scala has a better way of handling that:
for (i1 <- opt1; i2 <- opt2; i3 <- opt3) yield (i1, i2, i3)
But java has no support for generators.
Edit: btw. even Scala has `get()` and it was there a long time even when you need it even less there: http://www.scala-lang.org/api/2.11.8/index.html#scala.Option@get:A http://www.scala-lang.org/api/2.11.8/index.html#scala.Option...
- lmm 11y agoScala has grown organically and has a lot of things that are no longer idiomatic. Wartremover makes Option#get a compile error, and many recommend using it on new codebases.
- merb 11y agostill on java you don't have for generators so it will be really messy to have a big nested flatMap block.
- deleted 11y ago[deleted]
- masklinn 11y ago> actually that would be a bad idea. considering you want to handle multiple options at once. The proposal is to rename the method[0], not to remove it entirely. So the code you're showing would still be possible, only clearer that it's not innocuous (the body relies very very strongly on the conditional) [0] not sure why adding an override to #orElseThrow to throw a default exception is not on the table
- laxatives 11y agoThis pattern is nice, but it makes a lot of IDE's shit the bed completely. I'd really love to see a stronger relationship between Optional and Stream. Right now using both together is very ugly and the function name space is super fucked up, where there is an Optional.map and a Stream.map and you could have them nested or chained. I think there is some resolution in JDK9 for the latter issue though.
- merb 11y agoYeah actually the most patterns I miss in Java are actually pattern matching and generators. Everything else is actually "fine", I mean the var/val reduce some ugliness but that is just a trivial thing.