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.

A better (than Optional) maybe for Java

30 pointsby azhenley3 months ago

12 comments

_kidlike3 months ago
`then` is definitely less intuitive than `map` considering that you actually want to, well, map to something else.<p>Semantically, `then` would be equivalent to `ifPresent`, but, well, then both are intuitive.
评论 #42981628 未加载
ivan_gammel3 months ago
Interesting attempt, however:<p>1. There was no need to implement Set interface to support foreach loop. Iterable would be sufficient. Set is too specific and is not an obvious choice (for return type interoperability in subclasses sometimes it would be more convenient to have List).<p>2. Moving from isPresent&#x2F;get to try&#x2F;catch checked exception will produce worse result. Either you wrap ‘get’ immediately and get more verbose code, or you catch somewhere else and loose the context and readability. In “throws Exception” methods absence of stack trace will be painful.<p>3. Renaming ‘map’ to ‘then’ is too late. “Then” was used in functional style libraries more than 15 years ago, Java 8 settled the naming convention and it’s better to follow it.<p>4. What Optional is often missing is the context of empty state. It would be cool to have an implementation of None with a Throwable cause (and orElseThrow throwing it wrapped).
occz3 months ago
The real improvement is to lift nullability up to the type system. That way, every type acts as an optional by default, and the computer can infer when a nullable type has been null-checked.<p>This is what Kotlin does - it&#x27;s truly freeing to work with.
评论 #42983585 未加载
评论 #42991425 未加载
评论 #42981482 未加载
dehrmann3 months ago
&gt; The exception that is raised when an empty Maybe is used is a checked exception so you don&#x27;t forget to handle it.<p>I think they acknowledged get() should have been getOrThrow() so it&#x27;s clear what can happen, but I don&#x27;t know if making it checked is an improvement.<p>&gt; It implements the Collection interface, so it can be accessed with a for-loop.<p>I&#x27;m curious what the use cases are for this. I&#x27;ve used Optional, it can be annoying, it&#x27;s mostly around it being verbose and APIs being inconsistent. It was never wishing I could treat it as a collection.
评论 #42980546 未加载
评论 #42980569 未加载
stickfigure3 months ago
`map()` makes Optional congruent with java Streams. Optionals are very frequently used with Streams, eg:<p><pre><code> someList.stream().map(Thing::foo).findFirst().map(Other::bar)... </code></pre> And checked exceptions are a scourge. There&#x27;s a reason post-Java languages like C#, Scala, and Kotlin abandoned them. Even lombok offers @SneakyThrows.<p>The Java Optional&lt;?&gt; is pretty good as-is. The main improvement I&#x27;m make is that it needs to be Serializable. And woven into the core collection classes.
评论 #42981061 未加载
derriz3 months ago
I really dislike the use of exceptions - particularly checked exceptions - to handle control flow for things like this. I mean, it&#x27;s not EXCEPTIONAL to have an empty Optional, right? Their use here is an anti-pattern to my eye.<p>Java (the language) should just drop the notion of checked exceptions. It would be fully backward compatible as the checked&#x2F;unchecked distinction is a language feature not a JVM one.<p>They simply don&#x27;t work with Stream and cause abstraction leaks everywhere (e.g. you have to handle IOExpection for operations on StringWriter). It obfuscates flow control.<p>Anyway the problem with Optional is not going to be fixed by renaming API methods and fiddling like this (sorry to be harsh). The reason Optional is broken is because there is so much legacy APIs which use null to represent &quot;no result&quot; and until all of these APIs are changed&#x2F;deprecated, then Optional will never be a very useful tool.<p>As others have commented, the correct solution would be to ditch Optional completely along with the mass of flavors of @Nullable, @NotNull, etc that everyone uses along with having to use package_info.java everywhere and just make &quot;nullability&quot; part of the type system.<p>Kotlin got it right here. But it would be a breaking change in Java to make not-nullable the default. They could do it the opposite way to Kotlin - add a marker for &quot;not-nullable&quot; instead of one for &quot;nullable&quot; - e.g. &quot;String!&quot; would be a not-nullable String instead of say &quot;String?&quot; representing a nullable string.
评论 #43011042 未加载
评论 #42983549 未加载
评论 #42981901 未加载
评论 #42981953 未加载
blastonico3 months ago
&gt; The method names are more intuitive: e.g., then instead of map<p>But map makes total sense, considering that maybe is a monad.
评论 #42980655 未加载
评论 #42982351 未加载
评论 #42980892 未加载
评论 #42981386 未加载
评论 #42980512 未加载
stemlaur3 months ago
I would make the abstract class &quot;Maybe&quot; a sealed class with None and Some being the only permitted implementation So that clients can use pattern matching (enhanced switch)
评论 #42981791 未加载
dfe3 months ago
This seems to be worse in every way to Optional.<p>The throwing of a checked exception is ludicrous. It&#x27;s a classic case of abusing checked exceptions for flow control. The right approach is an isPresent() or isEmpty() call followed by a get() when you know it&#x27;s present. Static analysis can ensure you don&#x27;t screw this up.<p>Similarly, using then and thenMaybe instead of map and flatMap means it now has its own terminology different from Stream.<p>Being able to iterate it is dubious. You can already .stream() an Optional (to an empty or 1-element stream). You can also if (o.isPresent()) { o.get(); } which is hardly burdensome. I&#x27;m trying to figure out how a for-each loop is an improvement. But if you must: o.map(Collections::singleton).orElse(Collections.empty()). Or o.stream().toList().<p>I applaud the author for experimenting with different API designs, but this one is just not good. It causes more trouble than it solves.
Blackarea3 months ago
Checked exception in a Optional.get is really the worst idea that you could possibly come up with.<p>Have a look at vavr (<a href="https:&#x2F;&#x2F;github.com&#x2F;vavr-io&#x2F;vavr">https:&#x2F;&#x2F;github.com&#x2F;vavr-io&#x2F;vavr</a>) They did a fantastic job and the DX is so much better than anything java std has to offer. They have immutable equivalents for pretty much any java util data structure as well as either try option etc. To me the one lib that makes java actually feel fresh.
评论 #42981401 未加载
DarkNova63 months ago
I really hate the inclusion of a &quot;FastException&quot; and &quot;UnsafeUtils&quot; class.<p>Many developers will consistently make the wrong choice thinking their complex (but cold) execution path needs to be extra sleek and fast. But the reckoning comes during debugging (most likely by somebody else) where you have extra levels of indirection and non-expected violations of typically default behavior.
rubenvanwyk3 months ago
Or... Just use Kotlin?
评论 #42980897 未加载