Last active
September 15, 2022 01:55
-
-
Save CraigSiemens/8c32d833fd585bdb897087ce5e2bd5e9 to your computer and use it in GitHub Desktop.
An simple example of using resultBuilder to create an Array.
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
@resultBuilder | |
struct ArrayBuilder<Element> { | |
static func buildBlock(_ components: [Element]...) -> [Element] { | |
components.flatMap { $0 } | |
} | |
static func buildExpression(_ expression: Element) -> [Element] { | |
[expression] | |
} | |
static func buildEither(first component: [Element]) -> [Element] { | |
component | |
} | |
static func buildEither(second component: [Element]) -> [Element] { | |
component | |
} | |
static func buildArray(_ components: [[Element]]) -> [Element] { | |
components.flatMap { $0 } | |
} | |
static func buildOptional(_ component: [Element]?) -> [Element] { | |
component ?? [] | |
} | |
static func buildLimitedAvailability(_ component: [Element]) -> [Element] { | |
component | |
} | |
} | |
extension Array { | |
init(@ArrayBuilder<Element> content: () -> [Element]) { | |
self = content() | |
} | |
static func build(@ArrayBuilder<Element> content: () -> [Element]) -> Self { | |
content() | |
} | |
} |
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
// An Example of not using the result builder to make and array. | |
let shouldWave = true | |
let isDaytime = true | |
let names = ["Alice", "Bob"] | |
var greetingParts = [ | |
"Hello", | |
"World" | |
] | |
if shouldWave { | |
greetingParts.append("π") | |
} | |
if isDaytime { | |
greetingParts.append("βοΈ") | |
} else { | |
greetingParts.append("π") | |
} | |
for name in names { | |
greetingParts.append(name) | |
} | |
if #available(iOS 14.0, *) { | |
greetingParts.append("π") | |
} | |
print(greetingParts) // ["Hello", "World", "π", "βοΈ", "Alice", "Bob", "π"] |
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
// An Example of using the result builder to make an array. | |
let shouldWave = true | |
let isDaytime = true | |
let names = ["Alice", "Bob"] | |
let greetingParts = Array { | |
"Hello" | |
"World" | |
if shouldWave { | |
"π" | |
} | |
if isDaytime { | |
"βοΈ" | |
} else { | |
"π" | |
} | |
for name in names { | |
name | |
} | |
if #available(iOS 14.0, *) { | |
"π" | |
} | |
} | |
print(greetingParts) // ["Hello", "World", "π", "βοΈ", "Alice", "Bob", "π"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment