Proposal: "out" variables

Although you could use a tuple of return values to pass data back from a function, doing so when you have many things to pass back is less than ideal.

These “out” variables provide an alternative to multiple return values and must be uninitialized in both the formal and actual parameter contexts.

Example:

func f(out x:Int, y:Int) {
  let z = x + 3 // error as x is uninitialized
  x = 2*y // ok
}

let x1:Int
f(&x1, 2) // ok as x is uninitialized

let x2 = 3
f(&x2, 2) // error as x much be uninitialized

Why? Please provide an example of where this is "less than ideal" and how your solution would be more ideal.

In general:

- Non-toy example?
- Why is this better than using a tuple? Please justify with your example.
- Why is a new language feature, `out`, better than an `inout` function that runs an assertion for you?
- Why does this belong in the standard library and why can't you define it yourself?

Stephen

···

On Feb 28, 2016, at 1:20 PM, Amir Michail via swift-evolution <swift-evolution@swift.org> wrote:

Although you could use a tuple of return values to pass data back from a function, doing so when you have many things to pass back is less than ideal.