Last active
March 26, 2025 11:29
-
-
Save nickmain/689522b75f03037e5f44a472b690f889 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
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) | |
@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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment