"The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions" Caused by '+' op on Text() View

Inside my view's body mix in many other views, lines like this cause the compile error:

Text("Tax").foregroundColor(.secondary) + Text("\(self.taxPercent, specifier: "%.2lf")%")
Text("Tax").foregroundColor(.secondary) + Text("\(self.taxPercent, specifier: "%.2lf")%")

If there is only one line like this, compile...more than one, error...

What can I do to help the compiler?

The + operator must be too overloaded for the compiler to deal with inside the little bit slightly complicated view build.

I just call my own func instead:

func concatenate(_ lhs: Text, _ rhs: Text) -> Text {
    return lhs + rhs
 }

problem solve....

1 Like

SwiftUI is still in beta and therefore the compiler can sometimes give you very confusing or even wrong error messages. So I personally check all my code for mistakes when there's an error regarding SwiftUI.
I just wanted to say look out for mistakes in SwiftUI because at first I was really confused with the error messages.
Anyways, I'm glad you solved the problem on your own!

2 Likes

I'm not sure that SwiftUI should have added even more overloads for +, given the performance issues with the current set of overloads in the standard library.

1 Like

This is not only a problem with SwiftUI and/or beta versions of Swift. Confusing or wrong error messages related to type inference are a very common occurrence in highly generic Swift code, unfortunately.

4 Likes
Terms of Service

Privacy Policy

Cookie Policy