Created
July 10, 2025 09:43
-
-
Save omidgolparvar/d51bc9067653b25d4d867d733b0fb9ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| struct Output { | |
| let string: StringBuilder.Component | |
| } | |
| @resultBuilder | |
| struct StringBuilder { | |
| /// The type of individual statement expressions in the transformed function, | |
| /// which defaults to Component if buildExpression() is not provided. | |
| // typealias Expression = Double | |
| /// The type of a partial result, which will be carried through all of the | |
| /// build methods. | |
| typealias Component = String | |
| /// The type of the final returned result, which defaults to Component if | |
| /// buildFinalResult() is not provided. | |
| typealias FinalResult = Output | |
| /// Corresponding to the case where no component is used in the block. | |
| static func buildBlock() -> Component { | |
| print(Self.self , #function) | |
| return "" | |
| } | |
| /// Required by every result builder to build combined results from | |
| /// statement blocks. | |
| static func buildBlock(_ components: Component...) -> Component { | |
| print(Self.self , #function, components) | |
| return components.joined(separator: ", ") | |
| } | |
| /// If declared, provides contextual type information for statement | |
| /// expressions to translate them into partial results. | |
| static func buildExpression(_ expression: Double) -> Component { | |
| print(Self.self , #function, "Double:", expression) | |
| return "\(expression)" | |
| } | |
| static func buildExpression(_ expression: String) -> Component { | |
| print(Self.self , #function, "String:", expression) | |
| return expression | |
| } | |
| /// Enables support for `if` statements that do not have an `else`. | |
| static func buildOptional(_ component: Component?) -> Component { | |
| print(Self.self , #function, component ?? "-") | |
| return component ?? "" | |
| } | |
| /// With buildEither(second:), enables support for 'if-else' and 'switch' | |
| /// statements by folding conditional results into a single result. | |
| static func buildEither(first component: Component) -> Component { | |
| print(Self.self , #function, component) | |
| return component | |
| } | |
| /// With buildEither(first:), enables support for 'if-else' and 'switch' | |
| /// statements by folding conditional results into a single result. | |
| static func buildEither(second component: Component) -> Component { | |
| print(Self.self , #function, component) | |
| return component | |
| } | |
| /// Enables support for 'for..in' loops by combining the | |
| /// results of all iterations into a single result. | |
| static func buildArray(_ components: [Component]) -> Component { | |
| print(Self.self , #function, components) | |
| return components.joined(separator: ", ") | |
| } | |
| /// If declared, this will be called on the partial result of an 'if | |
| /// #available' block to allow the result builder to erase type | |
| /// information. | |
| static func buildLimitedAvailability(_ component: Component) -> Component { | |
| print(Self.self , #function, component) | |
| return component | |
| } | |
| /// If declared, this will be called on the partial result from the outermost | |
| /// block statement to produce the final returned result. | |
| static func buildFinalResult(_ component: Component) -> FinalResult { | |
| print(Self.self , #function, component) | |
| return FinalResult(string: component) | |
| } | |
| } | |
| let one = 1 | |
| let two = 2 | |
| let flag = true | |
| let str: Double? = 3.14 | |
| @StringBuilder | |
| func makeString() -> Output { | |
| "Hi:" | |
| 1.0 | |
| 2.0 | |
| 3.0 | |
| if one == two { | |
| 4.0 | |
| } else { | |
| 5.0 | |
| } | |
| if flag { | |
| 6.0 | |
| } | |
| if let str { | |
| str | |
| } | |
| } | |
| let string = makeString() | |
| print("string:", string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment