Swift 5.4 Result builders were first introduced as a semi-official language feature called โfunction buildersโ as part of the Swift 5.1 release. Xcode 12.5
The declaration of a result builder type doesnโt have to include any protocol conformance. ๐ Swift Reference
Result builders allow us to create a new value step by step by passing in a sequence of our choosing. ๐ Paul (๐ไพไธ)
A function builder is a type that implements an embedded DSL for collecting partial results from the expression-statements of a function and combining them into a return value[1]. ๐ Vadim
The function builders feature of Swift is described in Swift Evolution Proposal. The main goal of function builders is providing DSL like syntax.
๐ Swift with Majid
those buildEither() methods also enable switch statements to be used within result builder contexts, without requiring any additional build methods.
๐Sundell
@resultBuilderstructStringBuilder{ // โญ๏ธ (mandantory)staticfuncbuildBlock(_parts: String...)->String{ parts.joined(separator:"\n")} // โญ๏ธ support `if...else...` statementstaticfuncbuildEither(firstpart: String)->String{return part}staticfuncbuildEither(secondpart: String)->String{return part} // โญ๏ธ support `for...in` loopstaticfuncbuildArray(_parts: [String])->String{ parts.joined(separator:", ")}}// โญ๏ธ applied on `func`@StringBuilderfuncs1()->String{"Why settle for a Duke""when you can have""a Prince?"}// โญ๏ธ applied on `var`@StringBuildervar s2: String{"Why settle for a Duke""when you can have""a Prince?"}// โญ๏ธ translated by complier to:// ------------------------------// StringBuilder.buildBlock(// "Why settle for a Duke",// "when you can have",// "a Prince?"// )// ------------------------------print(s1())print(s2)// Why settle for a Duke// when you can have// a Prince?@StringBuildervar s3: String{"Why settle for a Duke""when you can have" // โญ๏ธ supported by `buildEither(first:)` / `buildEither(second:)`if .random(){"a Frog?"}else{"a King?"}}print(s3)@StringBuildervar countDown: String{ // โญ๏ธ supported by `buildArray(_:)`for i in(0...10).reversed(){"\(i)โฆ"}"Lift off!"}print(countDown)// 10โฆ, 9โฆ, 8โฆ, 7โฆ, 6โฆ, 5โฆ, 4โฆ, 3โฆ, 2โฆ, 1โฆ, 0โฆ// Lift off!