TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

What I miss in Java, the perspective of a Kotlin developer

3 点作者 nfrankel将近 3 年前

2 条评论

xandre_maxwell将近 3 年前
I tried so hard to get "into" kotlin when I had to use it at a job. It's just kinda boring. Maybe that's a good thing, though.
emccue将近 3 年前
These are pretty weak arguments<p>For the first point - val&#x2F;var<p>&quot;More importantly, it doesn’t offer its immutable counterpart, val. You still need to add the final keyword, which nearly nobody uses.&quot;<p>This super undermines the criticism. &quot;It has the feature but people don&#x27;t use it&quot; isn&#x27;t exactly much to write home about. Just use `final var` if you want. Make &quot;final var over var&quot; the same force culturally as JS&#x27;s const over let&quot;<p>&quot;However, the developers of Optional designed it for return values only. Nothing is available in the language syntax for methods parameters and return values. To cope with this, a bunch of libraries provides compile-time annotations:&quot;<p>This flow of causality here is slightly incoherent. The phrasing implies that if there was a version of optional that was designed for method parameters that would be better and there would be less of a need for compile-time annotations. Here it is<p><pre><code> sealed interface Option&lt;T&gt; { record Some&lt;T&gt;(T value) implements Option&lt;T&gt; {} record None&lt;T&gt;() implements Option&lt;T&gt; {} } </code></pre> This has &quot;better&quot; semantics than Optional for storing in fields and for using as method parameters. The existence of this does not obviate the desire to have null static analysis, so Optional really isn&#x27;t that related.<p><a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Ej0sss6cq14" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Ej0sss6cq14</a><p>It is true though that Kotlin has a more consistent story here, but there are active efforts to improve it for Java.<p>Also, the discussion of extension functions points out that static methods cannot be chained like instance methods - correct - but doesn&#x27;t really discuss the other end of the tradeoff. That is that there need to be rules for whether an extension function is &quot;in scope&quot; which can make local analysis for humans harder.<p>There are also other pretty trivial options for doing static method chaining if that is what you want stylistically.<p><pre><code> record Pipeline&lt;T&gt;(T value) { &lt;R&gt; Pipeline&lt;R&gt; map(Function&lt;? super T, ? extends R&gt; f) { return new Pipeline(f.apply(value)); } } ... final var result = new Pipeline&lt;&gt;(&quot;abc&quot;) .map(StringUtils::capitalize) .map(String::split) .value(); </code></pre> which, sure, is less pretty than<p><pre><code> val result = &quot;abc&quot;.capitalize().split(); </code></pre> But the rules about where StringUtils::capitalize comes from are far more amenable to code that is read, not written