Re:A deep dive into Swift’s function builders
by setr on Friday September 25, 2020 @12:23AM
(#24586565)
The example of...
static func buildIf(_ value: SettingsConvertible?) -> SettingsConvertible {
value ?? []
}
and static func buildEither(first: SettingsConvertible) -> SettingsConvertible {
first
}
static func buildEither(second: SettingsConvertible) -> SettingsConvertible {
second
}
?These just return their values -- the only one of note is that buildIf returns the empty array by default, but otherwise this is just defining an if statement to be an if statement.
It gives you however to make an if statement do something other than being an if statement, and that seems... bad.
You could for example define it as
static func buildEither(first: SettingsConvertible) -> SettingsConvertible {
[]
}
static func buildEither(second: SettingsConvertible) -> SettingsConvertible {
[]
}
I've been writing a lot of SwiftUI code since it came out - and a common use is like that example. Imagine a settings UI where you want to only show an advanced option if a switch/checkbox is toggled.