I have a functionBuilder
@_functionBuilder
struct MyBuilder {
static func buildBlock(_ numbers: Int...) -> Int {
var result = 0
for number in numbers {
result += number * 2
}
return result
}
}
Function
func myFunc(@MyBuilder builder: () -> Int) -> Int {
builder()
}
use
let a = myFunc {
10
20
}
print(a) // print 60 is work!
but
let b = myFunc {
10
}
print(b) // print 10?
Why is b not 20?
I try add other buildBlock
static func buildBlock(number: Int) -> Int {
return number * 2
}
But not working :(
Any idea?
Any idea?
What is happening in the failing case is that { 10 }
is being treated as a closure of type () -> Int
directly and the compiler doesn't appear to consider the function builder at all. The code that is produced is simply a function which returns 10
.
This appears to a "feature" where the recognition of { 10 }
as a simple closure overrides its possible recognition as a use of the function builder. This may just be a compiler issue or worse it might be a language definition problem...
Please head to feedbackassistant.apple.com and file a report.
来源:https://stackoverflow.com/questions/58409839/function-builder-not-working-when-only-one-value