TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Deprecating: java.util.Optional.get()?

53 pointsby redcodenlabout 9 years ago

11 comments

merbabout 9 years 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&#x2F;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):<p><pre><code> if (opt1.isPresent() &amp;&amp; opt2.isPresent() &amp;&amp; opt3.isPresent()) { int i1 = opt1.get(); int i2 = opt2.get(); int i3 = opt3.get(); } </code></pre> of course scala has a better way of handling that:<p><pre><code> for (i1 &lt;- opt1; i2 &lt;- opt2; i3 &lt;- opt3) yield (i1, i2, i3) </code></pre> 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: <a href="http:&#x2F;&#x2F;www.scala-lang.org&#x2F;api&#x2F;2.11.8&#x2F;index.html#scala.Option@get:A" rel="nofollow">http:&#x2F;&#x2F;www.scala-lang.org&#x2F;api&#x2F;2.11.8&#x2F;index.html#scala.Option...</a>
评论 #11589753 未加载
评论 #11589715 未加载
评论 #11590235 未加载
评论 #11589548 未加载
nv-vnabout 9 years ago
Having the get() method makes the entire purpose of Optional useless. All it does is make your null-checking code even more verbose. If you need to extract the value, the orElse method should be suitable on all cases. Deprecating get() should be an instant decision, since other than backwards compatibility it has nothing to add to the Optional class. Including it in the first place was as much of a mistake as the null pointer.
评论 #11589401 未加载
评论 #11589502 未加载
评论 #11589179 未加载
评论 #11589116 未加载
评论 #11589258 未加载
评论 #11590078 未加载
breakingcupsabout 9 years ago
So, can anyone explain to me how this:<p><pre><code> Database.readOrder() .map(OrderEngine::process) .filter(ProcessResult::succeeded) .ifPresent(Database::storeResult); </code></pre> Is more readable than the supposed &quot;unpleasant old skool&quot;:<p><pre><code> Order order = Database.readOrder(); &#x2F;&#x2F;can be null if(order != null) { ProcessResult result = OrderEngine.process(order); if(result != null &amp;&amp; result.succeeded()) { Database.storeResult(result); } } </code></pre> Maybe I&#x27;m &quot;old skool&quot; but I find the second much easier to read than the first.
评论 #11595238 未加载
评论 #11596741 未加载
willvarfarabout 9 years ago
The name getWhenPresent() doesn&#x27;t seem descriptive to me.<p>There is already orElse(value) and orElseThrow(exception), so why not add a plain orElseThrow() to raise the default exception?
评论 #11589420 未加载
specialistabout 9 years ago
I will never use Java&#x27;s Optional. I&#x27;m baffled why it even exists. Further, I remain unclear on the value (haha) of the @Nullable and @NotNull annotations. If it&#x27;s not baked into the language, why bother?<p>I&#x27;ve been using NullObject since (checking...) 1996. This is The Correct Answer.<p><a href="http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?NullObject" rel="nofollow">http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?NullObject</a><p>I first read about NullObject here:<p>Object-Oriented Design Heuristics <a href="http:&#x2F;&#x2F;amzn.to&#x2F;1ND1YSU" rel="nofollow">http:&#x2F;&#x2F;amzn.to&#x2F;1ND1YSU</a><p>What&#x27;d be really neat is some mojo to remove the boilerplate of implementing NullObjects.
评论 #11589619 未加载
评论 #11589553 未加载
cesarbabout 9 years ago
So Optional.get() is similar to Rust&#x27;s Option.unwrap(), right? A method with a short name, which newbies often use just more than they should, and which should only be used when it&#x27;s a bug if the Optional&#x2F;Option doesn&#x27;t contain a valid value at that point?<p>(I still don&#x27;t quite get the point of Optional; you&#x27;re replacing a direct pointer with a pointer to a pointer, but the outermost pointer can still be null, so you are still vulnerable to a NullPointerException, and now you have two &quot;not present&quot; values to check for: null and !isPresent().)
评论 #11589629 未加载
评论 #11589568 未加载
评论 #11589532 未加载
评论 #11589549 未加载
makecheckabout 9 years ago
This kind of thing is so frustrating because enormous effort has been put into solving exactly one VARIANT of a pretty <i>general</i> problem. The general case is: at a <i>particular</i> point in the code, is $THIS_DATA usable or not, for some appropriate definition of “usable” in that code?<p>And if “usable” has <i>any</i> additional meaning besides “is not null”, <i>this entire machinery is useless</i> because one STILL has to figure out the state of the data!<p>For example, what if you are sanitizing input from a user and there is a “wall” in your code beyond which a string is considered safe (e.g. decoded, evaluated by regex, or whatever is supposed to happen)? Or, what if “usable” means that a server has been connected to, or a database opened, or a calculation completed, or whatever else you want to say? What if it’s a numerical denominator and you want it to be nonzero? The list could go on and on, and none of these architectures deals with ANY of those possibilities.
评论 #11597426 未加载
评论 #11592777 未加载
strictfpabout 9 years ago
Why optimize for stupid usages of Optional?
raimille1about 9 years ago
Love it!! As a Java 7 developer transitioning to Streams this made no sense to me and ended up doing the optional.isPresent() -&gt; optional.get() ...<p>Took me to learn a pure functional language (Scala) to come back and start using map, filter, etc ... Not because Java 8 doesn&#x27;t support it, but because a functional language community just has that mindset. There are many Java 8 developers using streams() and optionals with an imperative programming mindset still.
dudulabout 9 years ago
Not sure `getWhenPresent()` is better. Maybe `unsafeGet()`? That&#x27;s a pattern found a lot on functional structures such as `IO` or `Task`.<p>There could be a discussion to remove `get` altogether, but I don&#x27;t think it would be a good idea. I don&#x27;t use the Java type, but `scala.Option` and I think `get` is occasionally helpful in unit tests, scripts, etc.
评论 #11589790 未加载
xerabout 9 years ago
What do they mean with &quot;god forbid a microservice&quot;?
评论 #11589095 未加载
评论 #11589114 未加载