Question: how can BehavioralRelay value be nullable?
See original GitHub issueVersion: 2.1.0 RxJava: 2.2.6
Since the default value is always there and not null
/**
* Constructs a BehaviorRelay with the given initial value.
* @param defaultValue the initial value, not null (verified)
* @throws NullPointerException if {@code defaultValue} is null
*/
BehaviorRelay(T defaultValue) {
this();
if (defaultValue == null) throw new NullPointerException("defaultValue == null");
value.lazySet(defaultValue);
}
and accept(value)
doesn’t accept null either
@Override
public void accept(T value) {
if (value == null) throw new NullPointerException("value == null");
setCurrent(value);
for (BehaviorDisposable<T> bs : subscribers.get()) {
bs.emitNext(value, index);
}
}
How can the value ever be null? 🙏 🤔
/**
* Returns a single value the Relay currently has or null if no such value exists.
* <p>The method is thread-safe.
*/
@Nullable
public T getValue() {
return value.get();
}
I don’t see anywhere value.lazySet
or value.set
or value =
is used in BehavioralRelay.java where it could cause the value to be missing either
Hence couldn’t answer my own question
How I use BehavioralRelay
val myRelay :BehavioralRelay<Int> = BehavioralRelay.createDefault(0)
//...
val latestValue = myRelay.value
// do stuffs with the latest value
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Behavior Relay accept Nullable Type in Kotlin #54 - GitHub
Is it possible that BehaviorRelay accept nullable type in kotlin and pass null in accept() or createDefault()?
Read more >Behaviour subject initial value null? - angular - Stack Overflow
The purpose of BehaviorSubject is to provide initial value. It can be null or anything else. If no valid initial value can be...
Read more >Null values and the nullable type - IBM
To indicate that a value variable is nullable, append a question mark (?) to the type when you declare it. This works in...
Read more >Design with nullable reference types - Microsoft Learn
This advanced tutorial provides an introduction to nullable reference types. You'll learn to express your design intent on when reference ...
Read more >BehaviorRelay (RxRelay 2.0.0 API)
Returns a single value the Relay currently has or null if no such value exists. Object[], getValues(). Returns an Object array containing snapshot...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Like @oldergod said, if you call
create()
the value will be null until one is supplied so the annotation is correct.Looks like an accidental holdover from reducing the RxJava
BehaviorSubject
sources: https://github.com/ReactiveX/RxJava/blob/24df131ecc012aa0591b9123b79a1f0f6a14a4f9/src/main/java/io/reactivex/subjects/BehaviorSubject.java#L320-L327Feel free to send a PR to change it!