Skip to content

Consider restricting Stream.ofNullable to non-null type arguments #97

@cpovirk

Description

@cpovirk

https://github.com/jspecify/jdk/blob/3669c24f878cf1dbaf42ccd75b002cdbc94fd4da/src/java.base/share/classes/java/util/stream/Stream.java#L1471C57-L1471C67

We could change the declaration of T to just <T>. That would be "simpler," and it could maybe in theory help someone who isn't realizing that the output is a Stream<@NonNull E> for some parametric-nullness type parameter E? But it might also annoy people who do actually want a Stream<SomeTypeThatMightIncludeNull>.

(TODO: Dig up past discussions of google/guava#1927 (comment) through the lens of nullness. The idea here would be that you can always map(t -> t) to change the type if you want to. [edit: Here we go: jspecify/jspecify#525. And I guess maybe this issue fits better in the jspecify/jspecify repo unless we do want to start using this repo's issues more?])

One thing to note is that the current signature works well for the Checker Framework for converting a Stream<@Nullable Foo> to a Stream<Foo> via flatMap(Stream::ofNullable). I imagine that the proposed signature would, too, but it would be good to check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions