Xcode Version 13.0 beta (13A5154h): Sequence.reduce(into:) call to `mutating func callAsFunction(...)` no longer compile

This doesn't compile anymore:

struct Counter {
    // directly mutate this on var works
    var count = 0
    // calling this on var do not compile
    mutating func callAsFunction(_ element: Int) {
        count += element
    }
    // this works!
    mutating func add(_ element: Int) {
        count += element
    }
}

#warning("This is reported SR-14250")
let result_0 = [1, 2, 3, 4, 5].reduce(into: Counter()) { $0($1) } // 👎 error: Cannot use mutating member on immutable value: '$0' is immutable
//                                                       ^^^^^^^
print(result_0)

#warning("This used to work, not anymore in Xcode Version 13.0 beta (13A5154h)")
let result0 = [1, 2, 3, 4, 5].reduce(into: Counter()) { counter, element in counter(element) } // 👎 error: Cannot use mutating member on immutable value: 'counter' is a 'let' constant
//                                                                          ^^^^^^^
print(result0)

// counter is indeed a var, see we can mutate it
let result1 = [1, 2, 3, 4, 5].reduce(into: Counter()) { counter, element in counter.count += element } // 👍 works
print(result1)

// counter is indeed a var, we can call mutating func on it
let result2 = [1, 2, 3, 4, 5].reduce(into: Counter()) { counter, element in counter.add(element) } // 👍 works
print(result2)

counter is inout, it's a var, not let...

It compile just fine if I don't use callAsFunction() syntax:

counter.count += element

so clearly counter is a var

Edit: existing bug https://bugs.swift.org/browse/SR-14250?filter=-2 where the shorthand parameters do not work. Now in the latest Xcode Version 13.0 beta (13A5154h), this doesn't work anymore with named closure parameter with callAsFunction syntax.

Cannot report bug ATM:
Screen Shot 2021-06-16 at 10.19.56 AM

Terms of Service

Privacy Policy

Cookie Policy