Why Can't the Swift Compiler Infer This Closure's Type

Swift Combine: Unable to infer complex closure return type error

Do what it asks you to do; tell it what type the closure is.

    let sub2 = generateIn()
.map { (value:Int) -> Int in
return Int(value)

Swift is unable to type infer variable containing closure that returns Bool

Swift can currently infer only single-return closures. You can rewrite your closure to:

let downloadNextPagination = { 
return current.integerValue < amount.integerValue && current.integerValue != amount.integerValue - 1

Add explicit type to flatmap

You didn't provide a whole lot of information about the classes involved so I mocked something up to create the playground example below.

The bit that I have added is in the closure of the flatmap operator. Basically I told the compiler what type that closure will return using the fragment input -> Future<[String], Error>. To come up with that line I had to know the return type of StorageManager.shared.saveAll(items: input) so I used the value my mocked up sample returns.

Your return value may be different.

import Foundation
import Combine

class StorageManager {
static let shared = StorageManager()

init() {

func saveAll(items: [String]) -> Future<[String], Error> {
Future<[String], Error> { fulfill in

struct NetworkManager {
static func getAll(path: String) -> Future<[String], Error> {
Future<[String], Error> { fulfill in
fulfill(.success(["Duck", "Cow", "Chicken"]))

let path : String = "some path"
let publisher : AnyPublisher<[String], Error> =
NetworkManager.getAll(path: path)
.flatMap { input -> Future<[String], Error> in

if path == "menus" {
print("It's a menu")

return StorageManager.shared.saveAll(items: input)

Alamofire Unable to infer closure type in the current context

func log() -> Self {
let responseSerializer = DataRequest.jsonResponseSerializer()
return response(responseSerializer: responseSerializer) { [weak self] response in
guard let _self = self else { return }

Works well.

Related Topics

Leave a reply