[swift-evolution] [Pitch] Don't require & for UnsafeRawPointer

Guillaume Lessard glessard at tffenterprises.com
Wed May 17 16:22:15 CDT 2017


Back to the initial example, here’s a variant that shows a “non-mutable” pointer lying; try it in a playground.

```
func get(_ pointer: UnsafePointer<Int>, at index: Int) -> Int
{
  let mutator = UnsafeMutablePointer(mutating: pointer)
  mutator[index] += 1
  return pointer[index]
}

let constantArray = [Int](repeating: 0, count: 3)

print(constantArray)	// [0,0,0]

let mutated = get(constantArray, at: 2) // returns 1

print(constantArray) // [0,0,1] oops!
```

I’d argue that this case should also require an ampersand and a mutable array.

Cheers,
Guillaume Lessard



More information about the swift-evolution mailing list