-
Notifications
You must be signed in to change notification settings - Fork 276
Refactor ValueWrapper into [value, valueOrNull, hasValue] #559
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
Conversation
Thanks for working on a solution! 😊 I gotta say I'm not the biggest fan of the whole "storing the value in multiple fields". It can make things overly complicated. More than that, I don't understand why Here's a more verbose example - try it in DartPad
As you can see, T can be nullable without the |
For convenience really, I noticed that for example list.firstWhereOrNull is very popular, also a lot of existing rxdart code probably has stream.value != null checks already. So either they just use value, or switch over to valueOrNull for the same behavior. HasValue is there, because you might want to safely check this first, before calling value. |
Your example only works when you pass a seed value, but the whole complexity lies with value being not nullable, but also not being seeded, If you check BehaviorSubject sources for example, you can see how cumbersome it is to support it. |
That is it. From my |
I just open PR #560, works as expected |
Yeah it's quirky. I'm gonna raise an issue with the Dart team about it. I'm sure they discussed it, and I'm interested in the outcome.
Not nullable AND not seeded? Ergo null? 😅 Let me ask you: Is it logical to support a field that is non-nullable, but should be able to store nulls (as it would if not seeded). Dart will NEVER allow that to happen. No matter how many custom errors or new fields you dream up, that is just not possible. And even if you find some kind of workaround, is that something you want? It clearly goes against the whole point of NNBD. 🙂
Then don't. What is gained from it? The consumer should be able to inject their own types. |
There's a solution for that in nnbd, it's called |
But I don't think I feel I'm failing in convincing you that this is not a good idea. I can live with a |
I meant as an analogy to Therefore, For me, it makes most sense that value is the exact same Type as the Stream Type, otherwise you might leak Type errors after consuming value in your code. |
The whole type system is there to prevent leaks. That shouldn't happen.
Having T be non-nullable is just not possible unless you seed it. So if we follow the idioms (and we should) set forth by static analysis we get: I'd love to be wrong on that! ValueStream is a weird beast to begin with: it promises (or tries to) that a value is available outside the context of the stream. It cannot ever make that promise (unless seeded). |
The point is, that Value exists because you want to peek at the current, last emitted value, which is expected to be of type Take |
Yeah that definitely should not be possible.
But I don't see a problem with value being nullable if not seeded. If not seeded I'd LIKE the type system to warn me about it: "Hey you, value might be null because the stream might not have emitted anything yet". I'd rather have a null (and the full help of the type system) than an exception thrown in my face which is:
EDIT: |
...Just a test to see if we can simplify things without requiring a
ValueWrapper
,this PR is not complete, but just an example of an idea:
instead of having a
ValueWrapper
: