<div dir="ltr">The object we were using flatMap on used to be optional. The fix was to use a map instead since it was no longer needed to be flatMapped. I agree that this would be a mis-use of flatMap but the inference behaviour here is still unexpected.<br><div><br></div><div>If the compiler doesn't support multi-statement closure type inference it would be nice for it to warn or error out in cases like this to enforce type, or it can also be handled by a linter/static analysis.</div><div><br></div><div>- Santiago</div><div><br><div class="gmail_quote"><div dir="ltr">On Sat, Oct 21, 2017 at 12:39 PM Martin R <<a href="mailto:martinr448@gmail.com" target="_blank">martinr448@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Compare <a href="https://bugs.swift.org/browse/SR-1570" rel="noreferrer" target="_blank">https://bugs.swift.org/browse/SR-1570</a> : Swift can infer the return type from single-statement closures, but not from multi-statement closures. Therefore in<br>
<br>
let result = strArr.flatMap { x in<br>
return x<br>
}<br>
<br>
the closure type is inferred as (String)-> String, and therefore the flatMap call matches exactly the<br>
<br>
func flatMap<SegmentOfResult>(_ transform: (Self.Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element]<br>
where SegmentOfResult : Sequence<br>
<br>
Sequence method, with Self.Element == String and SegmentOfResult == String, and flatMap returns [SegmentOfResult.Element] == [Character].<br>
<br>
In<br>
<br>
let result = strArr.flatMap { x in<br>
print(x)<br>
return x<br>
}<br>
<br>
the closure return type is not inferred from the closure itself, because it is a multi-statement closure. Here the compiler (apparently) chooses the<br>
<br>
public func flatMap(_ transform: (Element) throws -> String?) rethrows -> [String]<br>
<br>
Array method, and promotes `String` to `String?`. You would get the same result with an explicit return type in the single-statement closure:<br>
<br>
let result = strArr.flatMap { x -> String? in<br>
return x<br>
}<br>
<br>
But why do you use flatMap at all? If the intention is to map [String] to [String] then map() would be the right method:<br>
<br>
let strArr = ["Hi", "hello"]<br>
let result: [String] = strArr.map { x in<br>
return x<br>
}<br>
print(result) // ["Hi", "hello"]<br>
<br>
Regards, Martin<br>
<br>
<br>
> On 21. Oct 2017, at 02:50, Santiago Gil via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br>
><br>
> Hi Swift users,<br>
><br>
> I just ran into a bug after a Swift 4 migration where we expected [String] and got [Character] from a flatMap since flatMap will flatten the String since it's now a collection. While I understand why flatMap behaves this way now that string are collections, in testing this I found some weird behaviour...<br>
><br>
> var strArr = ["Hi", "hello"]<br>
><br>
> let result = strArr.flatMap { x in<br>
><br>
> return x<br>
><br>
> }<br>
><br>
><br>
> The type of results ends up being [Character] in the above case. However, adding a print statement changes things.<br>
><br>
><br>
> var strArr = ["Hi", "hello"]<br>
><br>
> let result = strArr.flatMap { x in<br>
><br>
> print(x)<br>
><br>
> return x<br>
><br>
> }<br>
><br>
> In this case, result is of type [String]<br>
><br>
> This seems like a bug, or is this expected Swift behaviour?<br>
><br>
> Thanks,<br>
><br>
> Santiago<br>
><br>
> _______________________________________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br>
</blockquote></div></div></div>