Improved Result Builder Implementation in Swift 5.8

The following code works fine before Swift 5.8:

protocol View { }

struct ArrayContent<Content: View>: View {
    let array: [Content]
}

@resultBuilder
struct ViewBuilder {
    static func buildBlock<Content: View>(_ content: Content) -> Content {
        content
    }
    
    static func buildArray<Content: View>(_ components: [Content]) -> ArrayContent<Content> {
        ArrayContent(array: components)
    }
}

struct MyView: View { }

func createView() -> some View {
    MyView()
}

@ViewBuilder
func test() -> some View {
    for _ in 0...10 {
        createView()
    }
}

print(test())

But with Swift 5.8, the compiler throws error:

@ViewBuilder
func test() -> some View {
    for _ in 0...10 {
        createView()
    } // <- Generic parameter 'τ_0_0' could not be inferred
}

It works if I change the return type of createView to a specific type (such as MyView in this sample). Is there something else I missed to get it to work with some View?

2 Likes