[swift-users] Compiler can't infer map transfer closure return type?

Neil Faiman neil.swift at faiman.org
Sun Mar 6 17:55:32 CST 2016

Using the version of Swift in Xcode 7.2.1, I compile the following program:

    let x1 = [1,2].map { _ in
        return "abc"

    let x2 = [1,2].map { _ in
        let x = "abc"
        return x

The assignment to x1 compiles successfully, but the assignment to x2 produces this error:

    Cannot invoke 'map' with an argument list of type '(@noescape (Int) throws -> _)'
    Expected an argument list of type '(@noescape (Self.Generator.Element) throws -> T)'

It seems that in both calls, the transform closure is returning a String, so I don’t see why the compiler can infer the return type in one case, but not in the other. Am I missing something obvious?


	Neil Faiman

More information about the swift-users mailing list