News for hackers. Stuff that matters.
Re:OpenJDK mailing list: Null-safety and value types by sam_lowry_ on Tuesday February 07, 2023 @05:46PM (#34700963)
What syntactic sugar they offer is not clear from the mail.

There are already lots of techniques, from annotations to Optional.ofNullable but none matches the simplicity of != null.

They can also go the route of `effectively final` and make the compiler deduce nullness instead of marking it explicitly.

Re:OpenJDK mailing list: Null-safety and value types by cogman10 on Tuesday February 07, 2023 @06:04PM (#34701191)
It'll allow the JVM to more aggressively optimize the code (the main benefit).

Annotations are, unfortunately, all over the board. There's a dozen different `NotNull` annotations out there. Having it in the language will make it the last solution to this problem.

You can't do deduction at compile time because you'll end up with different method signatures if someone calls a method with a null vs not. (not to mention the problem with someone calling these methods with reflection)

You can do deduction at runtime (and they likely will), but then you have to deal with the problem of unwinding an optimization when the hypothesis is proven wrong. That can be really costly when the optimization is dealing with memory layout. (Imagine an array of value types where you deduced they should be non-null, yet some unexpected path inserts a null. In that case you have to reallocate the entire array and it's elements in order to handle that).

It's more than just syntactic sugar.

Re:OpenJDK mailing list: Null-safety and value types by kaba0 on Tuesday February 07, 2023 @06:01PM (#34701159)
Discussions on the mailing list usually tries to avoid the exact syntax for as long as possible to avoid bike-shedding. But according to this mail there would be a default nullability (hopefully not-nullable as the default, but it may depend on whether it is a value class or not), and ! and ? as modifiers (and * for type variables).

So `Point p = null` won’t type check, you would have to mark it as `Point?` where Point is a value (primitive) class. There were also points that it should work with the usual type inference, e.g. `final var p = methodThatReturnsNullablePoint()` would be inferred to `Point?`.

Re:OpenJDK mailing list: Null-safety and value types by aardvark179 on Tuesday February 07, 2023 @08:12PM (#34702693)
Reference types probably can’t be made non-billable by default as that would break too much. But

  String! myString = “Definitely not null”;
doesn’t look too bad.

Edit: it’s worth saying that a lot of careful thought around compatibility goes into features like this. That means the core libraries should be able to transition to non-billable types where this is already a run time requirement, and existing compiled jars must continue to work.

Re:OpenJDK mailing list: Null-safety and value types by vips7L on Tuesday February 07, 2023 @09:10PM (#34703193)
I don’t think anyone would really use String! everywhere.
Re:OpenJDK mailing list: Null-safety and value types by aardvark179 on Wednesday February 08, 2023 @03:26AM (#34705567)
I think you’ll see it creep into parameter types in the core JDK and it will work its way out from there.
Re:OpenJDK mailing list: Null-safety and value types by kaba0 on Wednesday February 08, 2023 @12:45AM (#34704588)
I think it can work in a binary-additive way — the class file gets a new attribute for nullness information, while missing it (older class files) default to “unspecified”. That can be safely consumed by new code, which will get the updated syntax with a new option (specified on a module/package/class basis) where ‘T’ means ‘T!’ and that should be a proper subset of previously compiling code, so I don’t think anything would break that way. Similarly to how generics and unchecked warnings were rolled out.
Re:OpenJDK mailing list: Null-safety and value types by pjmlp on Wednesday February 08, 2023 @01:55PM (#34712906)
See the !! debacle for C#11 to see how changing the default went.

So now we are back at warnings if non nullable references are enabled on the compiler settings, and eventually we can turn them into warnings as errors.

The biggest issue is the large ecosystem of existing libraries.

Re:OpenJDK mailing list: Null-safety and value types by vbezhenar on Tuesday February 07, 2023 @06:02PM (#34701172)
I think the general idea is pretty clear.

`MyClass! myvar` is not supposed to accept nulls. Assigning null to `myvar` would throw exception, comparing `myvar` with null is pointless and probably should emit a warning. Or maybe even error.

`MyClass? myvar` is supposed to accept nulls. Dereferencing this value without checking for null is either warning or error.

`MyClass myvar` is supposed to be unspecified with regards to nulls. So basically current code. You can do whatever you want but you should refactor it to `MyClass!` or `MyClass?` some day.

Re:OpenJDK mailing list: Null-safety and value types by yarg on Tuesday February 07, 2023 @05:58PM (#34701114)
There's not really a nice way of implementing the syntax without changing the language.

Although perhaps they could resolve this with magic imports?

    import java.lang.magic.optional;

    public class Foo{
        public static void main(String...args){
            Foo? f = ...;
            if(f){
                f.doThings();
            }
            f.doThings(); //compile time error
        }
    }
The idea being that importing the magic class enables the cleaner syntax.
Re:OpenJDK mailing list: Null-safety and value types by usrusr on Wednesday February 08, 2023 @04:37AM (#34706046)
I doubt that it's possible to open the door for "language version by imports" without turning that decision into a forever-floodgate. Note that I am not entirely sure that it would be a bad decision (I do lean in that direction however), but it would definitely be a severe one.

It would be like a variant of inviting Dracula across the doorposts, only that this one may or may not actually be a cool guy and not a vampire, but in any case he will sure bring all his friends, and their friends and so on forever.

Re:OpenJDK mailing list: Null-safety and value types by yarg on Thursday February 09, 2023 @12:26AM (#34720107)
With great power comes great responsibility.