Different result type using Parameter Packs in a result builder

This tuple builder function returns a "flat" tuple (as I expect/hope):

func appendTo<each T, E>(tuple: (repeat each T), element: E) -> (repeat each T, String, E) {
    (repeat each tuple, "DIVIDER", element)
}

let firstTuple = appendTo(tuple: 1, element: "two")
let appendedTuple = appendTo(tuple: firstTuple, element: 3.3)
print(appendedTuple)           // (1, "DIVIDER", "two", "DIVIDER", 3.3)
print(type(of: appendedTuple)) // (Int, String, String, String, Double)

whereas the same function signature in a result builder returns nested tuples.

@resultBuilder
struct TupleBuilder {
    static func buildPartialBlock<V>(first: V) -> (V) {
        first
    }

    static func buildPartialBlock<each T, E>(accumulated: (repeat each T), next: E) -> (repeat each T, String, E) {
        (repeat each accumulated, "DIVIDER", next)

        // This crashes the Swift compiler:
//                appendTo(tuple: accumulated, element: next)
    }
}

func buildTuple<T>(@TupleBuilder _ builder: () -> T) -> T {
    builder()
}

let builtTuple = buildTuple {
    1
    "two"
    3.3
}
print(builtTuple)           // ((1, "DIVIDER", "two"), "DIVIDER", 3.3)
print(type(of: builtTuple)) // ((Int, String, String), String, Double)

Is this a bug?

BTW - attempting to use the appendTo(tuple:) function inside the buildPartialBlock(accumulated:) will crash the Swift compiler.

4 Likes

Still occurs with Xcode 26b2.
The unexpected output and the compiler crash.

It does work as expected if an intermediary type is introduced:

@resultBuilder
struct TupleBuilder {
    
    struct Pack<each T> {
        
        let tuple: (repeat each T)
        
    }
    
    static func buildPartialBlock<V>(first: V) -> Pack<V> {
        .init(tuple: first)
    }

    static func buildPartialBlock<each T, E>(accumulated: Pack<repeat each T>, next: E) -> Pack<repeat each T, String, E> {
        Pack(tuple: (repeat each accumulated.tuple, "DIVIDER", next))
    }
}

func buildTuple<T>(@TupleBuilder _ builder: () -> T) -> T {
    builder()
}

let builtTuple = buildTuple {
    1
    "two"
    3.3
}
print(builtTuple.tuple)           // (1, "DIVIDER", "two", "DIVIDER", 3.3)
print(type(of: builtTuple.tuple)) // (Int, String, String, String, Double)
1 Like

Thank you!

Adding

    static func buildFinalResult<each T>(_ component: Pack<repeat each T>) -> (repeat each T) {
        component.tuple
    }

is the final touch.

Now I can build a View that puts a Divider between each of its differently-typed children.

2 Likes