Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x BiConsumer nullability annotations violated #5216

Closed
davidschreiber opened this issue Mar 23, 2017 · 8 comments
Closed

2.x BiConsumer nullability annotations violated #5216

davidschreiber opened this issue Mar 23, 2017 · 8 comments

Comments

@davidschreiber
Copy link

RxJava 2.0.7

The BiConsumer interface has both parameters of its accept() method marked as @NonNull. However, when using the BiConsumer interface as result callback for a Single, the received parameter values are mutually exclusive being a non-null value and the other one being null. Although it seems to make sense in use, it violates the interface annotations and makes usage, at least, confusing.

Single.just("Some string")
    .subscribe(new BiConsumer<String, Throwable>() {
        @Override
        public void accept(@io.reactivex.annotations.NonNull String s, @io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
            assert s != null;
            assert throwable == null;
        }
    });

Single.<String>error(new RuntimeException())
    .subscribe(new BiConsumer<String, Throwable>() {
        @Override
        public void accept(@io.reactivex.annotations.NonNull String s, @io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
            assert s == null;
            assert throwable != null;
        }
    });
@akarnokd
Copy link
Member

Indeed, those use cases should override the default annotation with @Nullable. Would you like to submit a PR?

@akarnokd akarnokd added this to the 2.0 backlog milestone Mar 27, 2017
@bloderxd
Copy link
Contributor

bloderxd commented Apr 1, 2017

@akarnokd I can submit a PR if @davidschreiber can't, but what do you suggest? An exclusive BiConsumer for Single ?

@akarnokd
Copy link
Member

akarnokd commented Apr 1, 2017

That would be a breaking change. I see 3 options:

  • remove annotations from BiConsumer,
  • change annotations on BiConsumer to @Nullable - may introduce a lot of warnings,
  • Do nothing and have your IDE suppress/ignore that warning.

@bloderxd
Copy link
Contributor

bloderxd commented Apr 1, 2017

Well I don't think it's a breaking change, it's just an interface extending BiConsumer example:

  public void biconsumerWithCorrectNullableAnnotationInThrowable() {
        Single.just(1).subscribe(new SingleBiConsumer<Integer, Throwable>() {
            @Override
            public void accept(@Nullable Integer integer, @Nullable Throwable throwable) throws Exception {
                assertNull(throwable);
            }
        });
    }

What do you think?

@akarnokd
Copy link
Member

akarnokd commented Apr 1, 2017

Yes, you can simply delete the annotations or ignore them in your code.

@bloderxd
Copy link
Contributor

bloderxd commented Apr 1, 2017

Yes I know, but sometimes it's important to let developers know about property nullability, but ok thank you about your suggestions.

@PaulWoitaschek
Copy link
Contributor

PaulWoitaschek commented Apr 5, 2017

With kotlin it's actually more of an issue because if you create a biConsumer subclass it will take both parameters NonNull and throw an exception at runtime.

I think the correct solution here is to remove the annotation (makes sense, sometimes it's nullable, sometimes it it's nonNull, )

@akarnokd
Copy link
Member

akarnokd commented Apr 5, 2017

Closing via #5257

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants