print([ [1, 2], [3, 4]].flatMap{_ in })

- 375,296
- 67
- 796
- 848

- 71
- 5
-
2Replace `_ in` with `$0` and see what happens. – Joakim Danielson Aug 26 '21 at 13:28
1 Answers
One particular overload of flatMap
has been deprecated. It's the flatMap
method of Sequence
with this signature:
func flatMap<ElementOfResult>( _ transform: (Self.Element) throws -> ElementOfResult? ) rethrows -> [ElementOfResult]
And it has been deprecated since Swift 4.1. You can learn more about why it was deprecated by watching episode 10 of Point-Free, “A Tale of Two Flat-Maps”. (That episode is free to watch.)
However, the compiler is forced to pick that deprecated overload of flatMap
here.
For the non-deprecated version of flatMap
, the transform
function must return some type of Sequence
. That's the whole point of (non-deprecated) flatMap
: each element of the input is turned into some Sequence
, and those Sequence
s are concatenated (flattened) into a single output array.
But your transform
returns Void
, and Void
(which is an alias for the empty tuple, ()
) is not a Sequence
. So the compiler can't use the non-deprecated flatMap
.
The compiler can, however, implicitly promote your transform
to return an Optional<Void>
, which then lets the compiler use the deprecated flatMap
to compile the code.
The correct way to write your statement is to use map
instead of flatMap
, because you're returning a single value (()
) rather than a Sequence
:
[[1, 2], [3, 4]].map { _ in }

- 375,296
- 67
- 796
- 848