swift test2.swift
test2.swift:2:28: warning: 'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value
print(type(of: collections.flatMap { $0 }))
^
test2.swift:2:28: note: use 'compactMap(_:)' instead
print(type(of: collections.flatMap { $0 }))
^~~~~~~
compactMap
<<< invalid type >>>
If I go ahead and change it to compactMap, the program prints:
<<< invalid type >>>
I was hoping for it to print [Int].
FWIW, Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100). I am using a playground but behavior is the same for the command line.
However, any Collection<Int> does not itself conform to Sequence. And the compiler doesn’t open that existential to (effectively) some Collection<Int>, because (a) that only happens for arguments, not return values, and (b) if there were two elements in your outer collection, they might not have the same underlying type. So there’s not actually a type signature you could write for the transform closure that satisfies the requirements using only implicit conversions.
Unfortunately, the compiler then notices the old, deprecated flatMap, and you don’t get a diagnostic about any of what I just said.