[swift-users] inout params seem to have undefined behavior

Karl Pickett karl.pickett at gmail.com
Sat Jun 11 12:29:27 CDT 2016


I don't believe the (spartan) docs address this case:

func foo(inout a: [Int], inout b: Int) {
    let acopy = a
    a = [4, 5, 6]
    print(acopy)  // prints "[1, 2, 3]"
    b = 99
    print(a)   // prints "[4, 5, 6]"
    print(acopy)  // prints "[1, 2, 99]" (e.g. a let variable changed!)
}

var arr = [1,2,3]

foo(&arr, b: &arr[2])

print(arr)  // prints "[4, 5, 6]"


Is this code "undefined", meaning the spec / doc doesn't specifically
say what should be happening?  For instance, I can't believe a let
variable gets changed.  The docs also say inout changes the original
value when the function ends, not in the middle as is happening here.


More information about the swift-users mailing list