So for various reasons, our project has ended up with a bunch of signals of optionals. All over the place, we end up with:
.filter { $0 != nil }.map { $0! }
to deal with that.
Doesn't seem right, and forced unwrapping kills kittens.
I've added an unwrap() operator to Observable in our local project which turns Observable<Foo?> into Observable<Foo>, filtering out the nils. I'll put together a PR if y'all are interested. If there's an existing operator implementation I can key off of, that's great, too.
Hi @dpassage ,
if you are asking me how to solve that issue without !, this is one possible solution.
.flatMap { x in
return x.map(Observable.just) ?? Observable.empty()
}
I think that your solution could be more performant then this one, but you can benchmark it and see for yourself.
As far as for convenience operators, I would prefer not to include them for now. I'm afraid if we go that road this project will explode in code and size and we won't be able to maintain that amount of code or ship it without people complaining about the size.
There was some talk regarding creating a project like RxSuggar or something. I think that's probably something we could add to https://github.com/RxSwiftCommunity, we would just need to find maintainers for it first.
My 2 cents, maybe it would make sense to name it ignoreNil?
Hey, that's a cuter way to do it I hadn't thought of! I keep forgetting that map works on Optionals.
Understood about wanting to control operator explosion. I think this one is sort of unique to Swift because of how Optionals work.
I'll muse on making a 渭framework. Thanks!
I wonder if there's more elegant way to handle it by now.
@DragonCherry
I think this way is better now,
.flatMap { Observable.from(optional: $0) }
@tarunon Agreed, thanks!
.filterNil() does the job
Most helpful comment
@DragonCherry
I think this way is better now,