Avatar
//let a: Result<Int, any Error> = .success(2) //let b: Result<Int, any Error> = .success(3) // //func foo() throws -> Int //func foo() -> Result<Int, any Error> // //let sum: Result<Int, any Error> = a.flatMap { a in // b.flatMap { b in // .success(a + b) // } //} // //print(sum) let a: Task<Int, Never> = .init { return 2 } let b: Task<Int, Never> = .init { return 3 } extension Task where Failure == Never { func map<T>(_ body: @escaping (Success) -> T) -> Task<T, Never> { Task<T, Never> { body(await value) } } func flatMap<T>(_ body: @escaping (Success) -> Task<T, Never>) -> Task<T, Never> { Task<T, Never> { await body(await value).value } } } let square: Task<Int, Never> = b.map { $0 * $0 } print(await square.value) let sum: Task<Int, Never> = a.flatMap { a in b.map { b in a + b } } print(await sum.value)