2

I can't decipher this error message. This code compiles and runs

struct A {
    var x: Int
}
var a = A(x: 123)
let result = withUnsafePointer(to: &a) {
    print("ptr is \($0)")
    // return 1
}
print("result is \(result)")

But, if you uncomment that return 1 line, the compile fails. Xcode highlights the &a and says: Cannot pass immutable value of type 'inout A' as inout argument.

What does that mean? Maybe it's just a buggy error message?

(Xcode 8, beta 6)

Rob N
  • 15,024
  • 17
  • 92
  • 165
  • 2
    This most likely stems from the fact that the compiler can only infer the return type of a closure if it's a single expression, or from the calling context (see for example [Martin's answer on this Q&A](http://stackoverflow.com/a/34115788/2976878)) – adding an explicit type annotation allows your code to compile. However you should probably [file a bug report](https://bugs.swift.org) on that useless error message – Hamish Aug 27 '16 at 21:42
  • Thanks. Just filed the bug: https://bugs.swift.org/browse/SR-2501 – Rob N Aug 27 '16 at 21:47

0 Answers0