# Unexpected warning: 'flatMap' is deprecated

**URL:** <https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177>\
**Category:** Using Swift\
**Created:** [February 28, 2020, 10:48pm UTC](https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177 "2020-02-28T22:48:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [February 28, 2020, 10:48pm UTC](https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177/1 "2020-02-28T22:48:25Z")

</div>

I was trying out some things and ran into a warning I don't think should be happening:

```swift
let pythagoreanTriples =
    (1...).lazy.flatMap { z in
        (1...z).lazy.flatMap { x in
            (x...z).lazy.flatMap { y in // <- Warning on this line
                (x, y, z)
            }
        }
    }.filter { (x: Int, y: Int, z: Int) in x * x + y * y == z * z }

// warning:'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value

```

Changing flatMap to compactMap gets rid of the warning, but I don't think it's right. The code works correctly both ways.

Is this a bug or am I missing something? Swift version is 5.1.3

---

<div class="post-metadata">

**Author:** ![Braden\_Scothern](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/braden_scothern/32/1580_2.png) [@Braden\_Scothern](https://forums.swift.org/u/Braden_Scothern)\
**Post date:** [February 28, 2020, 10:54pm UTC](https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177/2 "2020-02-28T22:54:13Z")

</div>

You can use `map` for that final operation since it is converting `y` into `(x, y, z)`.

The use of `flatMap` when not working with another `Sequence`/`Collection` type is deprecated since it used to also do what `compactMap` does.

---

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [February 28, 2020, 11:01pm UTC](https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177/3 "2020-02-28T23:01:05Z")

</div>

> [@Braden\_Scothern](#):
>
> You can use `map` for that final operation since it is converting `y` into `(x, y, z)` .

Ah right, that makes sense. The warning put me on the wrong track, thanks!

---

<div class="post-metadata">

**Author:** ![mayoff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mayoff/32/613_2.png) [@mayoff](https://forums.swift.org/u/mayoff)\
**Post date:** [February 28, 2020, 11:02pm UTC](https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177/4 "2020-02-28T23:02:31Z")

</div>

The type of `(x...z).lazy` is `LazyMapSequence<(ClosedRange<Int>), Int.Type>`.

`LazyMapSequence` defines a non-deprecated `flatMap` method:

```swift
func flatMap<SegmentOfResult>(
    _ transform: (Element) throws -> SegmentOfResult
) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence

```

Note that `transform` here **must** return some sort of `Sequence`. In your code, you return `(x, y, z)`, which is a tuple, and tuples do not conform to `Sequence`. So Swift cannot use this `flatMap` method.

`LazyMapSequence` also defines a deprecated `flatMap` method:

```swift
func flatMap<ElementOfResult>(
    _ transform: @escaping (Element) -> ElementOfResult?
) -> LazyMapSequence<LazyFilterSequence<LazyMapSequence<LazyMapSequence<Base, Element>, ElementOfResult?>>, ElementOfResult>

```

Here, `transform` must return an `Optional` of a single output element. Since Swift can implicitly promote any non-optional value to an optional value, Swift can use this version of `flatMap` to compile your code.

The deprecated `flatMap` was deprecated by [SE-0187](https://github.com/apple/swift-evolution/blob/master/proposals/0187-introduce-filtermap.md).

---

<div class="post-metadata">

**Author:** ![orobio](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/orobio/32/5512_2.png) [@orobio](https://forums.swift.org/u/orobio)\
**Post date:** [February 28, 2020, 11:12pm UTC](https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177/5 "2020-02-28T23:12:20Z")

</div>

Thanks for the detailed explanation @mayoff, it's more clear to me now what happened exactly.

---

<div class="post-metadata">

**Author:** ![myintruder](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@myintruder](https://forums.swift.org/u/myintruder)\
**Post date:** [September 1, 2025, 2:38pm UTC](https://forums.swift.org/t/unexpected-warning-flatmap-is-deprecated/34177/6 "2025-09-01T14:38:01Z")

</div>

Did anything change in the meantime? Because I get the same warning for a case where I clearly return a sequence. I use Swift compiler 6.1.2.

```swift
struct TouchPosFeature {
    let position: CGPoint
    let timestamp: Date
}
let touchFeatures = [TouchPosFeature]()

let gestureDataRow = touchFeatures.flatMap({ touch in // warning: deprecated flatMap
	[touch.position.x, touch.position.y, touch.timestamp.timeIntervalSinceReferenceDate]
})

```

Edit: The values in the returned array are not of the same type, they are [CGFloat, CGFloat, Double]. I converted the last element into a CGFloat and the warning went away. But I still don't get why flatMap does't like when I return [Any].
