<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">The reasoning here was that the operator itself would be enough signal to tell you that it's mutating one of its arguments. "&amp;count += 1" would be a little too much.</div><div class=""><br class=""></div><div class="">Similarly, mutating methods take 'self' inout, but as noted before you aren't required to write 'mutating' at the call site, because it's assumed the name will be signal enough. This doesn't scale to arbitrary numbers of arguments, though, especially when you normally don't even think about some of your arguments being changed by a function call.</div><div class=""><br class=""></div><div class="">Mutating functions and assignment operators are fairly common (at least when not eschewing imperative programming). General inout parameters are not.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><div class="">P.S. At one point we thought about requiring you to declare "assignment" in your operator declaration, and disallowing inout parameters from arbitrary other operators, but that didn't actually turn into a proposal. It's not a bad idea, though—just a bit of compiler-enforced consistency.</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 29, 2016, at 15:48 , Erica Sadun &lt;<a href="mailto:erica@ericasadun.com" class="">erica@ericasadun.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I started poking around. It looks like the rules for calls with functions are different from those with operators. At least in the following example, the operator version doesn't need the &amp;. Any idea why?</div><div class=""><br class=""></div><div class="">-- Erica</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">//--------------------------------------------------------------</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// Stream Printing</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">//--------------------------------------------------------------</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">import</span> Darwin</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">import</span> Foundation</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span> StdErrStream: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">OutputStreamType</span> {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">mutating</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> write(string: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span>) {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>puts<span style="font-variant-ligatures: no-common-ligatures;" class="">(string.</span>cStringUsingEncoding<span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSUTF8StringEncoding</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)!)</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> stdStream = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">StdErrStream</span>()</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"hello world"</span>, toStream: &amp;<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">stdStream</span>)</div></div><div class=""><br class=""></div><div class="">vs</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">//--------------------------------------------------------------</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// MARK: Conditional Assignment</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">//--------------------------------------------------------------</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">infix<span style="font-variant-ligatures: no-common-ligatures;" class=""> </span>operator<span style="font-variant-ligatures: no-common-ligatures;" class=""> =? {}</span></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 76, 20);" class="">/// Conditionally assign optional value to target via unwrapping</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(0, 76, 20);" class="">/// Thanks, Mike Ash</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> =?&lt;T&gt;(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inout</span> target: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span>?) {</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> unwrapped = newValue { target = unwrapped }</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; min-height: 21px;" class=""><br class=""></div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">import</span> UIKit</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> image = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIImage</span>()</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> replacement = [#Image(imageLiteral: "Screen Shot 2016-01-29 at 4.35.48 PM.png")#]</div><div style="margin: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">image</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">=?</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIImage</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(named: </span>"DoesNotExist"<span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><br class=""></div><div class="">-- E</div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 29, 2016, at 4:23 PM, Jordan Rose via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">+1 from me, but I suggest finding some real-world examples of inout (and UnsafePointer) and showing those, rather than a made-up add1. The fact that we currently require '&amp;' for Unsafe-(immutable)-Pointer might make this a bit less nice than it otherwise would be.</div><div class=""><br class=""></div><div class="">(We've talked about allowing pass-by-value for UnsafePointer, but that has its own pros and cons, and should be discussed separately.)</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 29, 2016, at 14:44, Trent Nadeau via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><a href="https://github.com/tanadeau/swift-evolution/blob/master/proposals/00xx-use-inout-at-func-call-site.md" class="">https://github.com/tanadeau/swift-evolution/blob/master/proposals/00xx-use-inout-at-func-call-site.md</a><div class=""><br class=""></div><div class=""><pre style="" class=""># Use `inout` at Function Call Sites

* Proposal: TBD
* Author(s): [Trent Nadeau](<a href="http://github.com/tanadeau" class="">http://github.com/tanadeau</a>)
* Status: TBD
* Review manager: TBD

## Introduction

Currently when a function has `inout` parameters, the arguments are passed with the `&amp;` prefix operator. For example:

```swift
func add1(inout num: Int) {
    num += 1
}

var n = 5
add1(&amp;n) // n is now 6
```

This operator does not fit with the rest of the language nor how the parameter is written at the function declaration. It should be replaced so that `inout` is used in both locations so that the call site above would instead be written as:

```swift
add1(inout n) // symmetric and now obvious that n can change
```

*Discussion thread TBD*

## Motivation

The `&amp;` prefix operator is a holdover from C where it is usually read as "address of" and creates a pointer. While very useful in C due to its pervasive use of pointers, its meaning is not the same and introduces an unnecessary syntactic stumbling block from users coming from C. Removing this operator and using `inout` removes this stumbling block due to the semantic change.

This operator is also disconnected from how the function declaration is written and does not imply that the argument may (and likely will) change. Using `inout` stands out, making it clear on first read that the variable may change.

It is also possible that Swift may add Rust-like borrowing in the future. In that case, the `&amp;` symbol would be better used for a borrowed reference. Note that Rust uses the same symbol for declaring a borrowed reference and creating one, creating a nice symmetry in that respect of the language. I think Swift would want to have such symmetry as well.

## Detailed design

```
in-out-expression → inout identifier
```

## Alternatives Considered

Keeping the syntax as it currently is.</pre><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature">Trent Nadeau</div>
</div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>