4 ms·
Is it too much to ask for null safety in the type system?
by arein3 3y ago
Is it too much to ask for null safety in the type system?
- gavinray 3y agoNope not at all, here you go: https://openjdk.org/jeps/401 https://openjdk.org/jeps/401 It's part of Valhalla, as a pre-requirement for heap-flattening. Ctrl+F for "Null-restricted type". The strawman proposal syntax is "Foo!". When inlined, a null-restricted class type should have a heap storage footprint and execution time (when fully optimized) comparable to the primitive types. For example, a Point!, given the class declaration above, can be expected to directly occupy 128 bits in fields and array components, and to avoid any allocation in stack computations. A field access simply references the first or second 64 bits. There are no additional pointers. For a summary of current status and what it means for you as a developer, I recommend the linked email here and the author's comments in the thread: https://www.reddit.com/r/java/comments/13xtog3/valhallas_latest_plans_very_highlevel_accessible/ https://www.reddit.com/r/java/comments/13xtog3/valhallas_lat...
- arein3 3y agoThanks, hopefully it will be live sooner rather than later.
- ArturSkowronski 3y agoIn recent publication Brian Goetz explains why it is so complex to introduce one: https://mail.openjdk.org/pipermail/valhalla-spec-experts/2023-May/002276.html https://mail.openjdk.org/pipermail/valhalla-spec-experts/202... Great read. TLDR: It is hard to decide what the defaults should looks like, so they plan to add special markers (! and ?) to allow programmers to pass that detail to objects with "undefined nullability" (without ! or ?).
- gcau 3y agoAs a typescript developer who had to make something in java, it's so bizarre and alien to me that anything anywhere might be null, and I have to manually check if things are null everywhere, and then play a guessing game when I get a NPE. It feels like the most unsafe, unsound thing ever.
- kaba0 3y agoYou could add a few @Nullable annotations to your code where you might return null and be completely null-safe. Nullness is a so-called trivial property, so it can be statically analyised even in such a bolted on way.
- erik_seaberg 3y agoOn launch day, we absolutely should expect to see that. But after the industry spent 28 years producing a corpus that may or may not round-trip nulls, well, “I don’t have a solution but I have a lot of respect for the problem.”