<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="">I don't see why not. <div class=""><br class=""></div><div class="">Another way to potentially model it is like this:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""> protocol UnionType {</font></div><div class=""><font face="Menlo" class=""> func union(other: Self) -> Self<br class=""> mutating func union(other: Self) -> Self</font></div><div class=""><font face="Menlo" class=""> }<br class=""></font><br class=""></div><div class="">Internally, each of these is basically this:</div><div class=""><br class=""></div><div class=""><span style="font-family: Menlo;" class=""> func union(this, other: Self) -> Self</span><br style="font-family: Menlo;" class=""><span style="font-family: Menlo;" class=""> func union(inout this, other: Self) -> Self</span><br style="font-family: Menlo;" class=""></div><div class=""><br class=""></div><div class="">The above is suggesting that member functions have an implicit self parameter as the first parameter.</div><div class=""><br class=""></div><div class="">For value-types, it may even be possible to generate the mutating version.</div><div class=""><br class=""></div><div class=""> To call them:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""> a.union(b) // this is *always* the non-mutating one<br class=""> a&.union(b) // streamlined calling syntax, mutates `a`<br class=""></font></div><div class=""><br class=""></div><div class="">-David</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 15, 2016, at 11:11 AM, Greg Parker <<a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>> 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=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 12, 2016, at 9:18 PM, David Owens II <<a href="mailto:david@owensd.io" class="">david@owensd.io</a>> 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="">I kinda get where your coming from, but this really seems like a calling convention problem. You can have two functions that are nearly identical with the *only* difference being the modification of `self` versus returning a copy.<div class=""><br class=""></div><div class="">If we are really going to try and use a name, the suffix `InPlace` is atleast *always* consistent and never ambiguous, unlike nearly every attempt at using different pairs of noun and verb forms.</div><div class=""><br class=""></div><div class="">If we are willing to treat the calling syntax differently, then I think we can at least come up with a non-ambigous form. I think it even applies more generally throughout the language.</div><div class=""><br class=""></div><div class="">The two function signatures are this:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""> func union(other: Self) -> Self<br class=""> mutating func union(other: Self) -> Self</font></div><div class=""><div class=""><br class=""></div><div class="">However, the mutating version is now just syntactical short-hand for this:</div><div class=""><br class=""></div><div class=""><div class=""><font face="Menlo" class=""> static func union(inout this: Self, _ other: Self) -> Self</font></div><div class=""><br class=""></div><div class="">This changes the language to **not** allow a mutating function to be called with the “.” operator; after all, it’s really a static member now.</div><div class=""><br class=""></div><div class="">At the call site, you’d have the following:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""> var a: Set<Int> = [1, 2]</font></div><div class=""><font face="Menlo" class=""> let b: Set<Int> = [3, 4</font><span style="font-family: Menlo;" class="">]</span></div><div class=""><span style="font-family: Menlo;" class=""><br class=""></span></div><div class=""><span style="font-family: Menlo;" class=""> a.union(b) // this is *always* the non-mutating one</span></div><div class=""><span style="font-family: Menlo;" class=""> </span></div><div class=""><span style="font-family: Menlo;" class=""> Set.union(&a, b) // normal syntax for static methods, mutates `a`</span></div><div class=""><span style="font-family: Menlo;" class=""> a&.union(b) // streamlined calling syntax, mutates `a`</span></div><div class=""><span style="font-family: Menlo;" class=""><br class=""></span></div><div class=""><font face="Menlo" class=""> b&.union(a) // error: Cannot using mutating member on immutable value ‘b’.</font></div><div class=""><br class=""></div><div class="">This model works with class types as well, but suffers from all of the same limitations today with regards to the ability to enforce member mutation.</div><div class=""><br class=""></div><div class="">This brings two language changes:</div><div class=""><br class=""></div><div class="">1. Any function can be declared with the mutating modifier. This is syntactic sugar for a static function with the first parameter being an `inout` of Self. This works for both value and reference types.</div><div class="">2. Any static function that has an unlabeled `inout` parameter of `Self` can be invoked with a short-hand syntax of `&.` instead of the full static calling form.</div><div class=""><br class=""></div><div class="">Maybe there are some other limitations with this approach that I’m not thinking of at the moment.</div></div></div></div></div></blockquote><br class=""></div><div class="">Can you write both a mutating implementation and a non-mutating implementation of the same function with this scheme? Being able to provide both is important for performance.</div><div class=""><br class=""></div><br class=""><div class="">-- </div><div class="">Greg Parker <a href="mailto:gparker@apple.com" class="">gparker@apple.com</a> Runtime Wrangler</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>