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.

Null-Restricted and Nullable Types

224 pointsby lichtenberger10 months ago

21 comments

jeroenhd10 months ago
The difference between this approach and the approach C# took when they implemented this a few years ago is intereting. With C#, you enable nullability in a project, and every variable is declared as non-null unless you explicitly mark it as nullable (or use compiler directives to disable nullability for a block of code). With this proposal, all existing variables will be effectively nullable, explicitly nullable, or explicitly non-nullable.<p>Kotlin, another JVM language like Java, also follows the C# approach, assuming everything is non-null unless explicitly marked as being non-null, except Kotlin doesn&#x27;t have backwards compatibility to worry about. Kotlin also offers a special `lateinit var` declaration for leaving a non-nullable type unassigned until initialization in another method, throwing a special exception on uninitialized access, which is an interesting workaround for some code patterns that this approach doesn&#x27;t seem to cover.<p>I wonder why the choice to offer three options was made. Why not keep the non-annotated variables as nullable and make annotated variables explicitly non-null instead? I can&#x27;t think of a reason why you would want to declare something as nullable when not declaring anything automatically makes the type nullable anyway.<p>I think I like C#&#x27;s approach better, although this has the benefit of being usable in a legacy code base without having to solve any of the nullability issues. Then again, the C# approach does immediately highlight nullability issues in a code base, whereas this proposal hides them the same way they&#x27;ve always been hidden.<p>Additionally, I find this strange:<p>&gt; One method may override another even if the nullness of their parameters and returns do not match.<p>This feels like a footgun for when a developer overrides&#x2F;implements some kind of callback and returns null where the overridden method specifies non-null return values.
评论 #41137591 未加载
评论 #41138273 未加载
评论 #41138076 未加载
评论 #41137451 未加载
评论 #41137360 未加载
评论 #41137674 未加载
评论 #41137407 未加载
评论 #41137353 未加载
评论 #41137996 未加载
评论 #41137434 未加载
评论 #41138480 未加载
评论 #41213389 未加载
评论 #41138874 未加载
评论 #41152543 未加载
评论 #41137689 未加载
评论 #41140183 未加载
ajnin10 months ago
Looks good, finally a language-supported way to remove thousands of unnecessary Exceptions and null-checks. But the nullness-narrowing automatic conversions feel wrong. From the proposal :<p><pre><code> String? id(String! arg) { return arg; } String s = null; Object! o1 = s; &#x2F;&#x2F; NPE Object o2 = id(s); &#x2F;&#x2F; NPE Object o3 = (String!) s; &#x2F;&#x2F; NPE </code></pre> Surely at least the two first cases should yield a compiler error. The last case you&#x27;re explicit so it&#x27;s borderline but I&#x27;d rather see something like :<p><pre><code> String s = ... if (s != null) { # Here the compiler knows that the effective type is String! String! ss = s; # OK } </code></pre> Like this no possibility of error.
评论 #41140620 未加载
评论 #41138461 未加载
评论 #41138272 未加载
评论 #41137888 未加载
评论 #41137892 未加载
rvcdbn10 months ago
IMO there really needs to be a way to mark all variables non-null by default at package or at least a file level. Otherwise there will be a strong safety argument for always using T! syntax on almost every variable and this will just be a lot of noise.
评论 #41141229 未加载
评论 #41138897 未加载
评论 #41139868 未加载
sebstefan10 months ago
&gt;It is not a goal (at this time) to apply the language enhancements to the standard libraries<p>Speaking from experience being forced to use php, it&#x27;s a hassle to have ahead-of-time guarantees on your data that you need to strip out or build back up everytime you interact with the (massive) standard library<p>They should be a bit more enthused to add that kind of expressiveness to their STL and make it a first class citizen in the STL
评论 #41138978 未加载
评论 #41140646 未加载
评论 #41137581 未加载
评论 #41137323 未加载
dtech10 months ago
Would be cool if Java got this feature, explicit optionality at a language level a la T? is an enormous developer QoL in Kotlin and Typescript in my experience. In Java there&#x27;s tools like NullAway [1] but they&#x27;re a hassle.<p>Language-level support is leagues better than Optional&#x2F;Maybe in my experience too because it keeps the code focused on the actual logic instead of putting everything in a map&#x2F;flatMap railway.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;uber&#x2F;NullAway">https:&#x2F;&#x2F;github.com&#x2F;uber&#x2F;NullAway</a>
评论 #41137723 未加载
elric10 months ago
Archive link because the site is currently down: <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20240802081039&#x2F;https:&#x2F;&#x2F;bugs.openjdk.org&#x2F;browse&#x2F;JDK-8303099" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20240802081039&#x2F;https:&#x2F;&#x2F;bugs.open...</a>
评论 #41137515 未加载
评论 #41137454 未加载
Sankozi10 months ago
&quot;It is not a goal to require programs to explicitly account for all null values that might occur; unaccounted-for null values may cause compile-time warnings, but not compile-time errors&quot;<p>This is a bad decision. Java is mostly statically typed language, why introduce another dynamic behaviour? Hope there will be easy way to promote such warnings to errors.
评论 #41138014 未加载
kstenerud10 months ago
It&#x27;s unfortunate that we&#x27;re learning all of these lessons so late...<p>* things should be non-nullable by default<p>* things should be immutable by default<p>* things should be of the narrowest scope by default<p>So many design decisions for new things make the trade-off of immediate convenience instead of &quot;falling towards the safe path&quot; (which requires much more careful design and UX). And we have sooooo many footguns as a result in almost every language, platform, and technology thanks to this cowboy mentality.<p>Civil and electrical engineering have codes. We have lessons-learned, which we re-learn every 30 years or so with the next batch of languages and techs.
评论 #41138609 未加载
评论 #41138398 未加载
评论 #41138265 未加载
评论 #41138278 未加载
cletus10 months ago
Most of my work at Facebook was using Hack. Nullness is a key part of Hack&#x27;s stype system [1] and this solves so many BS errors. That&#x27;s not to say you couldn&#x27;t get a null where you weren&#x27;t expecting one, particularly because this was a later addition to the language so the code still had a lot of legacy &quot;mixed&quot; types that basically mean anything (mirroring the PHP roots where you could pass basically anything).<p>Some questions though.<p>First, what about nullable arrays? There are examples of String![] where the objects can be null but what about the array itself? As a reminder this is entirely legal in Java:<p><pre><code> String labels[] = null; </code></pre> Does that mean you have to declare it:<p><pre><code> String![]! labels; </code></pre> ?<p>In Hack, this is easy:<p><pre><code> vec&lt;String&gt; $foo; &#x2F;&#x2F; neither foo nor the elements are null ?vec&lt;string&gt; $foo; &#x2F;&#x2F; the elements are non-null but foo can be null vec&lt;?string? $foo; &#x2F;&#x2F; foo cannot be null, elements can be ?vec&lt;?string&gt; $foo; &#x2F;&#x2F; either can be null </code></pre> In practice, there&#x27;s really little reason to ever use a null array so the default really should be that something isn&#x27;t nullable. I understand the issue with Java is that all legacy code assumes nullability so that&#x27;s an issue.<p><pre><code> String? id(String! arg) { return arg; } String s = null; Object! o1 = s; &#x2F;&#x2F; NPE Object o2 = id(s); &#x2F;&#x2F; NPE Object o3 = (String!) s; &#x2F;&#x2F; NPE </code></pre> As for this example, shouldn&#x27;t (2) and (3) be compiler errors?<p>As for the last, I really like Hack&#x27;s as enforcement operators here rather than Java&#x27;s casting eg:<p><pre><code> class A {} class extends B {} void foo(B $b) {} ?B $b = new B(); foo($b); &#x2F;&#x2F; compiler error A $a = $b as A; &#x2F;&#x2F; runtime error if $b is null foo($a as B); &#x2F;&#x2F; runtime error if $a is not a B ?B $b2 = $a as ?B; &#x2F;&#x2F; $b2 is cast to B if it is one, otherwise null is returned </code></pre> Anyway, the question becomes can you retrofit this to the Java SDK? What does it look like for legacy code?<p>[1]: <a href="https:&#x2F;&#x2F;docs.hhvm.com&#x2F;hack&#x2F;types&#x2F;nullable-types" rel="nofollow">https:&#x2F;&#x2F;docs.hhvm.com&#x2F;hack&#x2F;types&#x2F;nullable-types</a>
评论 #41140233 未加载
dzonga10 months ago
seems all the good things about Kotlin are coming to Java now.<p>I will still prefer to work in Kotlin though as I don&#x27;t have to deal with things like lombok though Java records are nice.
评论 #41138702 未加载
评论 #41138151 未加载
sswezey10 months ago
The formatted JEP link: <a href="https:&#x2F;&#x2F;openjdk.org&#x2F;jeps&#x2F;8316779" rel="nofollow">https:&#x2F;&#x2F;openjdk.org&#x2F;jeps&#x2F;8316779</a>
评论 #41141077 未加载
mmaniac10 months ago
&gt; In this JEP, nullness markers are explicit: to express a null-restricted or nullable type, the ! or ? symbol must appear in source. In the future, it may be useful, for example, for a programmer to somehow express that every type in a class or compilation unit is to be interpreted as null-restricted, unless a ? symbol is present. The details of any such a feature will be explored separately.<p>I think this question needs to be answered now. The addition of ? is useless and confusing unless this is planned to eventually happen.<p>I am inclined not to introduce breaking changes and to only have !.
评论 #41138780 未加载
评论 #41139900 未加载
_ZeD_10 months ago
Finally! I tried many other solutions&#x2F;workarounds in the past years, like using the eclipse&#x2F;intellij notnull annotations and the various efforts of projects like <a href="https:&#x2F;&#x2F;www.lastnpe.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.lastnpe.org&#x2F;</a><p>I was wondering how &quot;enforceable&quot; these nullness check will be and how they will affect generics (will a &quot;List!&lt;String?&gt;&quot; be treated like a non-nullable list of nullable strings? and a List&lt;String!&gt; ? is the &quot;unknown nullables&quot; forwarded to the elements?)
评论 #41138083 未加载
cesarb10 months ago
One annoying detail of this proposal, is that it swaps the meaning of the nullness markers compared to Kotlin, which uses ! to mean &quot;it came from Java and had no nullability annotations so we don&#x27;t know&quot; and no marker to mean not nullable (<a href="https:&#x2F;&#x2F;kotlinlang.org&#x2F;docs&#x2F;java-interop.html#notation-for-platform-types" rel="nofollow">https:&#x2F;&#x2F;kotlinlang.org&#x2F;docs&#x2F;java-interop.html#notation-for-p...</a>).
评论 #41138724 未加载
评论 #41138060 未加载
评论 #41138286 未加载
Sohcahtoa8210 months ago
Can someone explain to me how nulls are even a problem? I know the <i>basics</i> of Java and did some basic projects in it for my CS degree, but never used it professionally or in a large project.<p>It seems to me that something somehow being assigned a null is a bug, but one that would stick out like a sore thumb. At some point, you&#x27;re returning a Null, right? Any code calling a function that can return a Null should know that being handed a Null is a possibility and handle it, right?<p>I&#x27;d think that eliminating Nulls is a bandaid over the wrong problem. Or is eliminating Nulls really meant to catch any potential NPE&#x27;s at compile time as a way for force better error checking?
评论 #41141660 未加载
评论 #41142060 未加载
评论 #41141771 未加载
elric10 months ago
Can&#x27;t say I&#x27;m a fan of the proposed syntax. Random question marks and exclamation marks do not improve the readability of the language. Introducing new symbols wouldn&#x27;t bother me as much if they made sense. E.g. square brackets make sense for arrays because they denote an area. Equal signs make sense for assignment and equality because of mathy history. But question marks and exclamation marks don&#x27;t make much sense for nullability.<p>Which is easier to understand? I don&#x27;t mind a few extra keystrokes if it improves readability.<p><pre><code> private String? foo; private String! bar; &#x2F;&#x2F; or private nullable String foo; private nonnull String bar;</code></pre>
评论 #41137409 未加载
评论 #41137435 未加载
评论 #41137542 未加载
评论 #41137428 未加载
评论 #41137420 未加载
评论 #41137543 未加载
评论 #41137588 未加载
评论 #41137521 未加载
评论 #41137402 未加载
评论 #41137403 未加载
评论 #41137744 未加载
评论 #41140363 未加载
评论 #41137383 未加载
评论 #41137735 未加载
评论 #41137630 未加载
exabrial10 months ago
Unfortunately, this breaks the language semantics and I don&#x27;t think it&#x27;s a very good idea. Java is a static language. If these are a huge problem in your codebase, you can discover the problems using nothing more than static analysis and healthy programming habits.<p>Personally I&#x27;ve never found nulls unintuitive or hard to use. Changing the core of the language to align itself with another fleeting in vogue trend is going to leave us with a bunch of garbage and tech debt when the trend inevitably falls out of style.
评论 #41139319 未加载
评论 #41139192 未加载
评论 #41139596 未加载
评论 #41139351 未加载
评论 #41140426 未加载
declan_roberts10 months ago
&gt; <i>In a Java program, a variable of type String may hold either a reference to a String object or the special value null. In some cases, the author intends that the variable will always hold a reference to a String object; in other cases, the author expects null as a meaningful value. Unfortunately, there is no way to formally express in the language which of these alternatives is intended, leading to confusion and bugs.</i><p>Casually just describes one of the largest sources of bugs in the history of programming.
rendaw10 months ago
It&#x27;s interesting to see this right on the tail of <a href="https:&#x2F;&#x2F;jspecify.dev&#x2F;blog&#x2F;release-1.0.0&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jspecify.dev&#x2F;blog&#x2F;release-1.0.0&#x2F;</a> by Meta et al. That was done AFAIK because JEP-305 died and it didn&#x27;t seem like Oracle was going to ever do anything about it. Did Oracle do nothing to coordinate here?
评论 #41141168 未加载
MBCook10 months ago
As a Java developer this is great. I’ve started to use the @Nullable and @NotNull annotations a lot for parameters and return values and they’re very helpful.<p>But they never made it into the language, so it’s only ‘enforced’ by the IDE and it’s a lot more visual noise than ? or ! which I’m already used to from other languages.<p>I can’t wait, I hope it gets in.
noelwelsh10 months ago
I think we&#x27;re still in the bashing rocks together stage of programming, but things like this definitely make me smile. Java is not an innovative language and the fact it is evolving towards something better shows that the industry as a whole is moving forward. We can&#x27;t be more than a decade away from everyone being able to program in a language with the features of ML circa 1983.
评论 #41137491 未加载
评论 #41214289 未加载
评论 #41137552 未加载
评论 #41137570 未加载
评论 #41137507 未加载
评论 #41137568 未加载