Fwd: Null-Restricted and Nullable Types concern
Anderson Vasconcelos Pires
andvasp at gmail.com
Tue Nov 19 11:22:04 UTC 2024
Hi Guys!
So, we get that adding null restriction to Java will significantly worsen
the fact that "nullable" is yet another bad default; this is pretty clear.
About "nullable" being a bad default. Have you thought for value classes
the default to be not nullable?
Like the == comparison that is being "fixed" where value objects compare if
they are of the same class and have the same field values.
If we think in the long future to "fix" the default to be not nullable,
this would be a step.
Thinking about reading code, maybe it is not a good choice to have this
difference between value and identity objects because we would have to
check the variable type to understand the logic.
Obviously not for the first versions!
Thanks!
Anderson.
On Fri, Nov 15, 2024 at 1:55 PM Brian Goetz <brian.goetz at oracle.com> wrote:
> Before anything makes it into the JDK I'd like to see the plan for
> this
>
>
> This is a valid opinion, but let's be clear what you're saying: it means
> you think that having a solid, committed flippening story is so important
> that you're willing to say that "no one can have the benefits of valhalla
> for another 5-10 years, or maybe ever." That's a pretty big deal. (And,
> yes, sometimes we are willing to make such tradeoffs, but it's still a
> pretty big deal.)
>
> But, maybe that's not what you meant. Maybe what you meant is "I want to
> impress on you how important I think it is to get to where the nullity
> default is flipped." If that's what you mean, yes, we understand how
> strongly some people feel about this. Our antipathy to attacking this
> problem *right now* is not because we don't think it is a real pain point,
> but one of practicality: it's not possible to solve all the problems
> simultaneously and still deliver something in a mortal lifetime.
>
> Or maybe what you meant is "there seem to be simple solutions to this
> problem, why wouldn't we jump on them?" Unfortunately just because a
> solution is obvious (like "let's just put a bang on the module directive"),
> doesn't mean they are simple. (To be clear, you are asking for what
> would be, by far, the biggest change to Java ever -- reinterpreting
> existing code to mean something else based on a directive _in another
> file_.) Rest assured that we've seen these "solutions" proposed before,
> and our lack of enthusiasm for them is that they may well create more
> problems than they solve.
>
> So, we get that adding null restriction to Java will significantly worsen
> the fact that "nullable" is yet another bad default; this is pretty clear.
> But if we really thought that we can't do Valhalla until we reinterpret all
> the world's Java code with a new default, the rational response might well
> be to cancel the project. Sometimes things have to be separated out into
> smaller, dependent chunks; sometimes we even have to accept that we might
> not even get to all the later chunks.
>
> I get that you would be more comfortable if we had a committed,
> bulletproof plan for this. But I think I can say with confidence that we
> won't; we need to focus our attention on the many nearly-impossible
> problems we already have in front of us before adding more to our plate.
>
>
>
>
> On 11/15/2024 11:16 AM, Caleb Cushing wrote:
>
> It is not a goal to automatically re-interpret existing code—use of these features should be optional and explicitly opted in to (future work will explore mechanisms to request a bulk opt-in without needing to change individual types)
>
> Before anything makes it into the JDK I'd like to see the plan for
> this, especially since I do not believe such a feature should be
> released unless it has been applied to the entirety of the JDK.
> Without this the adoption will be almost as slow as JPMS (an aside on
> that I'd like progressive enforcement there so it could be possible to
> get there).
>
> Not to speak for anyone other than myself, the community seems to be
> leaning towards the conversion where non null is the default, and
> using marker annotations on packages,classes,modules. This or
> something like this to speed conversion would be appreciated. maybe
> the following (Evil tangent: let this only exist on module, I'd like
> to get modules actually moving, in a different email it'd be nice if
> jars without at least an automatic module would start failing to load)
>
> module org.example! {} // or ?
> package org.example.something!; // or ?
>
> could be used for speedy defaults on otherwise unmarked code. Some
> have taken to separating API and Fields though.
>
> How do these behave? given the following interface, static
> implementations (meaning they aren't doing real loads from an external
> database), that Stream::map arguments are marked "?" as nullable and
> use
>
> interface MyRepository {
> Stream<Foo?>! findFoos();
> }
>
> class Foo {}
>
> given
>
> class MyRepositoryValueNeedsNullCheck implements MyRepository {
> Stream<Foo?>! findFoos() {
> return Stream.of(new Foo(), null);
> }
> }
>
> class MyRepositoryValueDoesNotNeedNullCheck implements MyRepository {
> Stream<Foo?>! findFoos() {
> return Stream.of(new Foo());
> }
> }
>
> when MyRepositoryValueNeedsNullCheck
> findFoos().filter(Objects::nonNull).map( foo -> foo.toString())
>
> when MyRepositoryValueDoesNotNeedNullCheck
> findFoos().map( foo -> foo.toString())
>
> will the above be considered "ok" since neither of them would actually
> create a runtime error? they should be considered ok in my opinion.
>
> class MyRepositoryValueNeedsNullCheck implements MyRepository {
> Stream<Foo?>! findFoos() {
> return /// do something with a database or something that
> cannot be known until runtime
> }
> }
> when MyRepositoryValueNeedsNullCheck
> findFoos().filter(Objects::nonNull).map( foo -> foo.toString())
> findFoos().map( foo -> foo.toString())
>
> does the return on findFoos() do an implicit runtime check for the
> Stream throwing some good NPE error? will the map without the filter
> result in a compile time NPE?
>
> class MyRepositoryValueNeedsNullCheck implements MyRepository {
> Stream<Foo!>? findFoos() {
> return null;
> }
> }
>
> is this valid? I have a valid use case for what's more or less the
> inverse (where the interface is nullable, but the implementation is
> not)
>
> Will we be getting a way to assert variables with inferred types?
>
> `var! foo = null` (tangent: can we reopen mutability on var here?
> meaning val! foo or const! foo, that thing I foolishly voted
> against... and was wrong, it should exist (I hadn't used a language
> that had it yet, maybe I wasn't the only one), I don't like const
> because I see that as a static final)
>
> given
>
> Foo! foo;
> foo = new Foo();
>
> valid so long as it's assigned before use?
>
> is partial markup really valid at compile time?
>
> interface MyRepository {
> Stream<Foo?> findFoos();
> }
>
> I find the examples using this, but then Stream is nullable? it feels
> like if you define any nullability on a given type then the whole type
> should be fully marked up.
>
> In all of these stream examples, the concept in checker framework is
> @PolyNull, will there be a way of writing functional interfaces that
> defines that the implementers can use them in a PolyNull fashion?
> meaning the null check necessity can't be known until usage is
> defined?
>
> Will the compiler strip if null checks out of the bytecode if it
> determines they are no longer necessary? probably not necessary to
> define in the jep.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20241119/644cabd8/attachment-0001.htm>
More information about the valhalla-dev
mailing list