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