[swift-users] Compiler can't infer map transfer closure return type?
Dennis Weissmann
dennis at dennisweissmann.me
Mon Mar 7 03:44:08 CST 2016
Hi Neil,
indeed the compiler should be able to infer the type here.
The error message in the latest development snapshot (swift-DEVELOPMENT-SNAPSHOT-2016-03-01-a) is a little better:
> Expression type '[_]' is ambiguous without more context
You can work around like this:
let x2 = [1,2].map { _ -> String in
let x = "abc"
return x
}
or
let x2: [String] = [1,2].map { _ in
let x = "abc"
return x
}
- Dennis
> On Mar 7, 2016, at 12:55 AM, Neil Faiman via swift-users <swift-users at swift.org> wrote:
>
> 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?
>
> Thanks,
>
> Neil Faiman
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160307/f7ae16bc/attachment.html>
More information about the swift-users
mailing list