-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
2.x: emitting nulls from Observable.create() #4492
Comments
We adopted the Reactive-Streams style of value management which forbids using nulls in the sequences. Use public static Observable<Object> clicks(final View view) {
return Observable.create(emitter -> {
MainThreadDisposable.verifyMainThread();
view.setOnClickListener(ignored -> emitter.onNext(ignored)); // works fine
emitter.setDisposable(new MainThreadDisposable() {
@Override
protected void onDispose() {
view.setOnClickListener(null);
}
});
});
} |
How about providing a class in RxJava itself which basically provides that |
Java has a fantastic built-in type and instance that you can ignore at your will: |
I agree with @vanniktech. I think that providing a RxJava type which express this intent will lead to more consistent apis. Because @akarnokd you have suggested to use an Integer/Object, but I was thinking in creating my own Type, like a wrapper for Void, something like So I suggest that we should create a new RxJava |
I do not think this is RxJava's responsibility. Kotlin has Unit and with On Wed, Sep 7, 2016, 8:42 AM Víctor Albertos notifications@github.com
|
Precisely, to make completely clear that the value doesn't matter without needing to check the docs. @JakeWharton, what are you planning to do regarding this matter for RxBinding? Will be |
Probably On Wed, Sep 7, 2016 at 8:52 AM Víctor Albertos notifications@github.com
|
Not worth adding to the RxJava 2 branch, but I suspect I'll end up using something similar to this in my client libraries:
Just because I want to encode the behaviour as a type and it always succeeds equality checks as Void did, even if the cost is a tiny bit higher than using any other constant. |
I think more users are going to feel this gap too, and they will eventually create his own type. Indeed, I'm thinking to create something like that for my own libraries and client code. But it would be better if someone with enought reputation/popularity -like @JakeWharton or @akarnokd - created a library to fill this gap. |
I'm a bit fed up with all these reactive types because each new one shamed out of me doubles the work I have to do for free and overtime. There are infinite amount of base reactive types possible with all sorts of property combinations, no-valued types, multi-valued onNext, with or without backpressure, multiple onErrors, etc. |
JetBrains (extremely reputable and popular) already created a library On Wed, Sep 7, 2016 at 10:46 AM Víctor Albertos notifications@github.com
|
@akarnokd I don't think the |
It's the same problem as with Pairs, Tuples and function combinators; we don't use them in the library and would just clutter the namespaces for projects which use something else anyway. You can use your own 'ignored' type as you see fit. public enum MyIgnored {
PLEASE_IGNORE_ANY_VALUE_OF_ME;
} |
I understand how a stream of null values can be emitted using a type that represents null. I don't understand how a stream of values that might have null instance emitted should be handled without loosing type safety as Java does not handle union types. |
@vietj Use |
@akarnokd seems the best alternative indeed |
It is not possible right now to create an observable that emits
null
s usingObservable.create()
.Emitter
's javadoc states@param value the value to signal, not null
and in fact when I tried callingemitter.onNext(null)
it is checked at runtime and results in aNullPointerException
.Can I get any insight as to why this decision has been taken? And why only in this place, as I believe it is still possible to emit
null
via for exampleObservable.just()
?It is a fairly common pattern when you want an observable that emits multiple "events" that don't carry any data. Usually then you create an
Observable<Void>
and emitnull
s inonNext()
. For example this is what I tried to write to get an observable of clicks on a view in Android (while @JakeWharton 's RxBinding doesn't have a 2.x release):Should I use a different way of creating such observable? Or is there a different pattern altogether for this use case in 2.x?
The text was updated successfully, but these errors were encountered: