Simple ForEach gets dreaded compiler is unable to type-check this expression in reasonable time

I am trying to use a simple ForEach loop to draw circles for a collection of points. I am flabbergasted that the compiler borks on something so simple, so I suspect a bug in my code, but I cannot see it.

This is all done within a View in a GeometryReader in which I have several let constants that compute correct positions.

If I do the following, it works, but the circles are all bunched on the left side of the view since the position is not scaled properly:

    let points: [CGPoint] = curve.makePoints()
   // Text("  \(points[0].x),  \(points[0].y)  \(points[1].x), \(points[1].y)")
    ForEach(points, id: \.self.x) { point in
      Circle()
        .position(point)
        .frame(width: shapeSize, height: shapeSize)
}

The commented out Text did show the correct values of the first two points.

However, if I scale the position to where I want it to be, as below, I get the dreaded error. The only change between code that builds and runs and the code that breaks the compiler are in these two code blocks.

    ForEach(points, id: \.self.x) { point in

      var nnextX: Double = xOffset + pixWidth * ((point.x - xminRounded) / xtot)
      var nnextY: Double = Double(proxy.size.height) - yOffset - pixHeight * ((points.y - yminRounded) / ytot)
      var nextPoint:CGPoint = CGPoint(x: nnextX, y: nnextY)
      Circle()
         .position(nextPoint)
       // .position(point)
        .frame(width: shapeSize, height: shapeSize)

  }

I know the point.x value is unique so I can use it as the id:

I know the logic for computing "nnextX" and "nnextY" are correct since when I used very similar computations in a

Path { path in
...
              for i in 1..<curve.xValues.count {
                let nextX: Double = xOffset + pixWidth * ((curve.xValues[i] - xminRounded) / xtot)
                let nextY: Double = Double(proxy.size.height) - yOffset - pixHeight * ((curve.yValues[i] - yminRounded) / ytot)
                let nextPoint:CGPoint = CGPoint(x: nextX, y: nextY)
                path.addLine(to: nextPoint)

              }

All the lines were drawn in the proper place in my View.

Finally, the developer documentation shows 3 ways to initialize a CGPoint: with a Double, with an Int, or with a CGFloat, so I should be good there.

Any ideas?

Where exactly do you get the error message ? Which line ?

Often, this is because compiler has too many type inference to test. Expliciting types usually solves it.

What are xOffset, pixWidth… types ?

Try this just to see if it solves the error:

ForEach(points, id: \.self.x) { point in
      var nnextX: Double = Double(xOffset) + Double(pixWidth) * ((Double(point.x) - Double(xminRounded)) / Double(xtot))
      var nnextY: Double = Double(proxy.size.height) - Double(yOffset) - Double(pixHeight) * ((Double(points.y) - Double(yminRounded)) / Double(ytot))
      var nextPoint:CGPoint = CGPoint(x: nnextX, y: nnextY)
      Circle()
         .position(nextPoint)
       // .position(point)
        .frame(width: shapeSize, height: shapeSize)

  }

20 seconds after I posted this, I saw my error. The line for "nnextY" has "points.y". That should be point.y.

Sigh. It works now.

Sorry for the wasted bandwidth. (Although the compiler could have pointed out the problem, don't you think?)

All the lines were drawn in the proper place in my View. Please show the whole definition of the View to confirm that.

You close a thread by clicking on the "correct answer" icon (the rounded √ sign)

Take care to mark the correct answer, you cannot change once done.

Simple ForEach gets dreaded compiler is unable to type-check this expression in reasonable time
 
 
Q