Very simple SwiftUI code: tiny change cause "The compiler is unable to type-check this expression in reasonable time"

What' s wrong with my code?

import SwiftUI

struct Breathe: View {
    static private let colors = [Color(#colorLiteral(red: 0.3529411765, green: 0.662745098, blue: 0.6745098039, alpha: 1)), Color(#colorLiteral(red: 0.4078431373, green: 0.7450980392, blue: 0.6549019608, alpha: 1)), Color(#colorLiteral(red: 0.4666666667, green: 0.8196078431, blue: 0.631372549, alpha: 1)), Color(#colorLiteral(red: 0.5179253817, green: 0.8318992257, blue: 0.6992306113, alpha: 1)), Color(#colorLiteral(red: 0.4078431373, green: 0.7333333333, blue: 0.6509803922, alpha: 1)), Color(#colorLiteral(red: 0.3568627451, green: 0.6666666667, blue: 0.6745098039, alpha: 1))]
    
    @State private var leafSize: CGFloat = 50
    
    var body: some View {
        ZStack {
            //          v .... if 6 is changed to Self.colors.count, error below
            ForEach(0..<6, id: \.self) { index in
                Circle()
                    .fill(Self.colors[index])
                    .opacity(0.5)
                    .offset(x: -self.leafSize / 2)
                    .rotationEffect(.degrees(Double(index * 360 / Self.colors.count)))
            }
        }
        .frame(width: self.leafSize, height: self.leafSize)   // <== The compiler is unable to type-check this expression in reasonable time
    }
}

My ForEach is wrong maybe?

Edit: seems there is nothing wrong with ForEach, if I just add

static private var count: Int { Self.colors.count }

and do

ForEach(0..<Self.count, id: \.self) { index in

No problem.

0..<Self.colors.count works fine with me :thinking:.

I'm on Xcode 11.4 beta 3...are you using the same? I hope not...for my sanity sake

Xcode 11.3.1... heh

Oh, something to do with Xcode 11.4 beta 3 then, not my code...

...anyway, I ran out of time already...I only have one hour to complete this simple task and I failed :roll_eyes:

I don’t think you failed at all. Battling with the compiler can be frustrating- especially when using beta tools.
For what it’s worth, your take on the challenge was very similar to mine. I also spent a good deal of time battling the tools, partly because I haven’t used SwiftUI a whole lot, and partly because I also chose the beta tools.
I would encourage you to keep playing around even though you spent the first hour already. I found it so rewarding to play around with the near instantaneous feedback and getting a bit deeper into the intricacies of SwiftUI, animations, etc.

1 Like