I'm trying to wrap my head around @_functionBuilder
. There is one case I haven't been able to figure out.
I put together this simple example, when there are two passengers this works great. But when there is only 1 I get this error:
error: FunctionBuilder.playground:21:5: error: cannot convert value of type 'Passanger' to closure result type '[Passanger]'
@_functionBuilder
struct PassangerBuilder {
static func buildBlock(_ passangers: Passanger...) -> [Passanger] {
return passangers
}
}
struct Passanger {
let name: String
}
struct Car {
let passangers: [Passanger]
init(@PassangerBuilder _ builder: () -> [Passanger]) {
self.passangers = builder()
}
}
Car {
Passanger(name: "Tom")
// Passanger(name: "Mary")
}
My solution is add more init function with single item Passanger return to struct Car
. It will be:
struct Car {
let passangers: [Passanger]
init(@PassangerBuilder _ builder: () -> [Passanger]) {
self.passangers = builder()
}
init(@PassangerBuilder _ builder: () -> Passanger) {
self.passangers = [builder()]
}
}
Hope to help you
For anyone else that ends up here.
As I answered on the question referenced by CRD, there is a bug on the current implementation of function builders where it ignores the function builder when there is only one value available. It's fixed on Swift 5.3 and available on Xcode 12.
Thanh Vu's solution of making a overload works for now, but you don't even need the @PassangerBuilder
annotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With