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

Chris Lattner clattner at apple.com
Mon Mar 7 12:06:31 CST 2016


> On Mar 7, 2016, at 1:44 AM, Dennis Weissmann via swift-users <swift-users at swift.org> wrote:
> 
> 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:

Yep, further improvements are tracked by:
<rdar://problem/24209966> QoI: When a non-single-expression closure type can't be inferred, say that we didn't look at the body
<rdar://problem/22123191> QoI: multi-line closure with failure to infer result type should add a fixit

-Chris

> 
>> 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 <mailto: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 <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users
> 
> _______________________________________________
> 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/44e2b060/attachment.html>


More information about the swift-users mailing list