This isn't really a bug but more of a question. I'm trying to implement some custom filters and I'm not sure where I'm going wrong.
public extension ObservableType {
@warn_unused_result(message="http://git.io/rxs.uo")
public func notNil() -> Observable<E> {
return self.filter { $0 != nil }
}
}
public extension ObservableType where E == String {
@warn_unused_result(message="http://git.io/rxs.uo")
public func notEmpty() -> Observable<E> {
return self.filter { $0.characters.count > 0 }
}
}
The goal is to be able to write
someObservable
.notNil()
.subscribe
Instead of
someObservable
.filter { $0 != nil }
.subscribe
It's really not a huge deal to just write filter but I figured I'd ask since I'm missing something here.
Hi,
I think that compiler doesn't know
public extension ObservableType where E: OptionalType { // <-- you have to create and implement OptionalType and make Optional conform to it
@warn_unused_result(message="http://git.io/rxs.uo")
public func notNil() -> Observable<E> {
return self.filter { $0.hasValue }
}
}
... that E is comparable with nil, so there are ways to tell him that (creating an protocol OptionalType and conforming Optional to it).
Thanks! This seems to be working.
public protocol OptionalType {
func hasValue() -> Bool
}
extension Optional: OptionalType {
public func hasValue() -> Bool {
return (self != nil)
}
}
public extension ObservableType where E: OptionalType {
@warn_unused_result(message="http://git.io/rxs.uo")
public func notNil() -> Observable<E> {
return self.filter { $0.hasValue() }
}
}
public extension ObservableType where E == String {
@warn_unused_result(message="http://git.io/rxs.uo")
public func notEmpty() -> Observable<E> {
return self.filter { $0.characters.count > 0 }
}
}
Closing this out thanks for the help again!
No problem :)