struct Accumulator {
var value = 0
mutating func doIt(_ n: Int) {
value += n
}
mutating func callAsFunction(_ n: Int) {
doIt(n)
}
}
let result1 = [1, 2, 3, 4].reduce(into: Accumulator()) {
$0.doIt($1) // 1) call mutating is fine here
}
.value
let result2 = [1, 2, 3, 4].reduce(into: Accumulator()) {
$0($1) // 2) error: cannot use mutating member on immutable value: '$0' is immutable
}
.value
Looks like a bug .
So it appears I cannot use the shorthand $0($1). It works if I explicitly declare the parameters:
let result2 = [1, 2, 3, 4].reduce(into: Accumulator()) { (a: inout Accumulator, n: Int) in
a(n)
}
.value
but I think it should work with shorthand params, so you are right it's a bug? Should i file a bug report?
I got different error "type of expression is ambiguous without more context"
but $0.callAsFunction($1)
fix the problem.
I'm using Xcode 12.5 b2 Xcode Playground, what are using?
Upon closer look, I got ambiguous type as well on Playground 3.4 (the iOS app).
It should compile & run. If anything, it shouldn't behave too differently from $0.callAsFunction($1)
(which compiles & runs).
Version 12.4 (12D4e).
but as
said, it's so weird $0($1) != $0.callAsFunction($1)
...
Only 5.2 works
5.0, 5.1, 5.3, nightly fail with:
error: type of expression is ambiguous without more context
}.value
error is different for Xcode Version 12.5 beta 2 (12E5234g):
error: cannot use mutating member on immutable value: '$0' is immutable
That's even stranger.
So it wasn't working, then in 5.2 work, then broken again, this seems to indicate there is no test case for this. Otherwise, this breakage would be noticed and not allow to go out broken.
Or these is test case, but the failure is ignored...