I was trying out some things and ran into a warning I don't think should be happening:
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
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:
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.
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.
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].