I'm trying to extend optionals into something readable, and achieved this so far:
@discardableResult
func isNotNil(_ handler: (Wrapped) -> Void) -> Optional {
switch self {
case .some(let value):
handler(value)
return self
case .none:
return self
}
}
@discardableResult
func isNil(_ handler: () -> Void) -> Optional {
switch self {
case .some:
return self
case .none:
handler()
return self
}
}
So that I can call my functions on an optional such as:
viewModel?.title.isNotNil { _ in
//do something
}.isNil {
//handle error
}
The problem is, I want to reuse these functions to return specific types, which I'm not able to achieve or am missing something out. For example:
let vm: MyViewModel = dataSource?.heading.isNotNil {
return MyViewModel(title: $0.title, subtitle: $0.subtitle)
}
I've been brainstorming on this and would love some help around this.
Thanks!