Ambiguous reference to member &&

If I want to calculate if everything is Boolon the list truewith this snippet, why the types will not be correctly inferred?

let bools = [false, true, false, true]
let result = bools.reduce(true, combine: &&)
+4
source share
1 answer

I ran into the same error a while ago (but then with ||). If you want to use reducefor this, the easiest solution would be to write

let result = bools.reduce(true, combine: { $0 && $1 })

or

let result = bools.reduce(true) { $0 && $1 }

instead of this. As stated in the comments, you can also use

let result = !bools.contains(false)

Not only is it more readable, but also more efficient, because it will stop at the first meeting false, and not iterate over the entire array (although the compiler can optimize this).

+1

All Articles