[swift-users] Type inference of array element type

Toni Suter tonisuter at me.com
Fri Mar 24 14:21:00 CDT 2017


Ok, I submitted a bug report (https://bugs.swift.org/browse/SR-4347 <https://bugs.swift.org/browse/SR-4347>) and I'll try to fix it in the
next few days.

Thanks and best regards
Toni

> Am 24.03.2017 um 18:51 schrieb Mark Lacey <mark.lacey at apple.com>:
> 
>> 
>> On Mar 24, 2017, at 3:08 AM, Toni Suter via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> Hi,
>> 
>> If I declare a variable and initialize it with an array literal whose elements are integer literals and nil literals,
>> the compiler will infer the type Array<Optional<Int>> for that variable:
>> 
>> let arr = [1, nil, 3]
>> print(type(of: arr)) 	// Array<Optional<Int>>
>> 
>> However, that only works with nominal types such as Int and String. If I do the same thing with an array of tuples,
>> I get a compile error:
>> 
>> let arr = [(1, false), nil, (3, true)]		// error: type of expression is ambiguous without more context
>> print(type(of: arr))
>> 
>> Why can't the compiler infer the type Array<Optional<(Int, Bool)>> in this example? Is there a reason for this or is it a bug?
> 
> Offhand it seems like we should be able to properly handle this. Can you open a bug report at bugs.swift.org <http://bugs.swift.org/>?
> 
> Mark
> 
>> 
>> Thanks and best regards,
>> Toni
>> 
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170324/9494b338/attachment.html>


More information about the swift-users mailing list