I’m thinking if optional return type is a good idea.
My main concern is that if observed value is of optional type itself, then Element will be inferred by removing a level of optionality. And returning nil will terminate iteration, instead of being one of the observed values.
Pretty much in all the use cases I can think of, subscribers store a strong reference to the sender and sender maintains a list of weak references to the subscribers. IMO proposal overestimates importance of observing weak references.
But I guess being able to stop iteration from the sender side still might be useful in some cases. I’m not ready to argue for complete removal of this feature.
With this in mind, I think it would be better to have two initializer overloads:
- one taking closure that returns
Elementand cannot stop iteration early - another that can, but closure returns a dedicated enum type that cannot be confused with
Optional.
// 1
Observed { person.maybeName }
// 2
Observed { [weak person] in
guard let person else { return .finish }
return .next(person.maybeName)
}