Skip to content

Instantly share code, notes, and snippets.

@LeeKahSeng
Created March 9, 2021 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LeeKahSeng/ff0bfddc51412b3b288c26c89fcc8489 to your computer and use it in GitHub Desktop.
Save LeeKahSeng/ff0bfddc51412b3b288c26c89fcc8489 to your computer and use it in GitHub Desktop.
@resultBuilder
struct StringBuilder {
static func buildBlock(_ components: String...) -> String {
let filtered = components.filter { $0 != "" }
return filtered.joined(separator: "⭐️")
}
static func buildOptional(_ component: String?) -> String {
return component ?? ""
}
static func buildEither(first component: String) -> String {
return component
}
static func buildEither(second component: String) -> String {
return component
}
static func buildArray(_ components: [String]) -> String {
return components.joined(separator: "")
}
static func buildExpression(_ expression: Int) -> String {
return "\(expression)"
}
static func buildExpression(_ expression: String) -> String {
return expression
}
static func buildFinalResult(_ component: String) -> Int {
return component.count
}
static func buildFinalResult(_ component: String) -> String {
return component
}
}
@StringBuilder func greet(name: String, countdown: Int) -> String {
for i in (0...countdown).reversed() {
i
}
"Hello"
if !name.isEmpty {
"to"
name
} else {
"World"
}
}
print(greet(name: "Swift Senpai", countdown: 5))
@StringBuilder var greetCharCount: Int {
"Hello"
"World"
}
print(greetCharCount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment