[swift-evolution] Replace named returns with `out` parameters?

Kevin Ballard kevin at sb.org
Thu Dec 29 17:17:37 CST 2016


I know you've already decided against this, but I think it deserves an
explanation for why the two are different.


Tuples in function parameters was basically a special case to the type
system. The function type was effectively modeled as taking one argument
which was a tuple, and a function of multiple arguments just had a multi-
element tuple. But this wasn't actually true. For one thing, you could
have a single named argument, but you cannot have a 1-element tuple
(named or no), which means the function argument isn't _really_ a tuple.
For another, this caused weird behavior when invoking functions with a
single tuple element. I don't remember the specifics anymore (I'm not
sure I ever truly understood the rules around this), but in some cases
you could pass a single tuple to a function expecting multiple
arguments, but in other cases you couldn't.


So ultimately, getting rid of the notion that a function's arguments is
a tuple was actually a simplification to the type system and to the
internals, and made the rules a lot more straightforward.


Now contrast that with the return type. The return type is just a single
value of *any* valid type. And a tuple is a valid type. Therefore, the
return type can be a tuple. That's a straightforward consequence of the
basic rules of the type system. Restricting it so functions couldn't
return a tuple would be a complication to the type system, and a rather
weird one at that. And that's without even considering the complexity
and syntactical issues with your proposed `out` parameter.


-Kevin Ballard



On Wed, Dec 28, 2016, at 03:09 AM, Anton Zhilin via swift-evolution wrote:
> Some people on the list wondered, why we have moved from tuples in
> function parameters, but multiple returns are still implemented using
> tuples? The following code still compiles:


> *func* *position*() -> (x: Int, y: Int) {  *return* (x: , y: ) }

> *let* (y, x) = position()
>
> (Maybe it’s a bad example, because naturally we’d use Point struct.
> Let’s pretend those two parameters don’t make sense as a struct.)
> What I want to discuss is if we should introduce out parameters *and*
> make them the default for multiple returns for Swift 4. The syntax
> would look like:


> *func* *position*(x: out Int, y: out Int) { x =  y =  }

> *var* y position(x: *let* x, y: y)
>
> out arguments can be any patterns allowed on the left side of
> assignment, including wildcard pattern and tuple destructuring
> pattern.
> 

> _________________________________________________

> swift-evolution mailing list

> swift-evolution at swift.org

> https://lists.swift.org/mailman/listinfo/swift-evolution


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161229/401f3660/attachment.html>


More information about the swift-evolution mailing list