<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="">Thanks a lot Hooman, I understand better now.</div><div class=""><br class=""></div><div class="">The part that tripped me was the fact that value types would be copied every time. </div><div class=""><br class=""></div><div class="">I didn’t realise I wasn’t making a copy of the value type in my program, the same instance of the value type was getting passed (as Quinn and you had pointed out).</div><div class=""><br class=""></div><div class="">Thanks and regards,</div><div class="">Muthu</div><div class=""><br class=""></div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On 31 Jul 2017, at 4:27 AM, Hooman Mehr <<a href="mailto:hooman@mac.com" class="">hooman@mac.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><br class="Apple-interchange-newline">On Jul 24, 2017, at 2:38 AM, somu subscribe via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">Thank a lot Quinn, your solution to use inout works well without crashing.</div><div class=""><br class=""></div><div class=""><b class="">Question 1:</b></div><div class="">- Also changing <font color="#0433ff" face="Menlo" class="" style="font-size: 11px;">Helper</font> to a <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">class</span></font> doesn’t seem to crash. Is that a solution that wouldn’t cause a crash or just works by chance ?</div></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">It is not by chance. It illustrates the key difference between classes and structs: class instances are passed by reference because they have identity. The identity of an object never mutates: If you change `Helper` to a class, then you can also change `helper` property from `var` to `let` and still change the value of `v1` inside `helper`. If this is not clear to you, I recommend you first get a better understanding of differences between value types and reference types and especially learn about<span class="Apple-converted-space"> </span><i class="">object aliasing</i><span class="Apple-converted-space"> </span>before deciding which approach is the right approach for you. </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><b class="">Background:</b></div><div class="">Just a little background into what I was trying to achieve (<u class="">I could be wrong</u>):</div><div class=""><br class=""></div><div class="">- I have a set of classes <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">C1</span></font>, <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">C2</span></font>, <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">C3</span></font> which has a lot of common code</div><div class=""><br class=""></div><div class="">- I would like to build something that can be reused without exposing the implementation details. (I can subclass but would expose the underlying functions, same applies to <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">protocol</span></font> as well)</div><div class=""><br class=""></div><div class="">- I thought I would build helper <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">class</span></font> / <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">struct</span></font> which would contain the common code. I can make the helper a <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">private property</span></font> so that the functions wouldn’t be exposed to the instances of <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">C1</span></font>, <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">C2</span></font>, <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">C3</span></font>. In order to achieve that I had to pass some functions from <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">C1</span></font> into the <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">Helper</span></font> <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">struct</span></font>.</div><div class=""><br class=""></div><div class=""><b class="">Question 2:</b></div><div class="">- Is this problem (hiding implementation details) normally tackled using <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">Helper class</span></font> (or <font color="#0433ff" face="Menlo" class=""><span class="" style="font-size: 11px;">struct</span></font>) or is there a more better approach ?</div></div></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">It depends on what you mean by hiding and whether those classes sharing common implementation details also share a common public API (protocol) or ancestry (shared superclass). </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Generally, what you call `Helper` is the correct way to go if the following is true: </div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Your `Helper` makes logical sense as something coherent with its own meaningful properties, so that it can be given a specific name that makes sense (besides a very broad name such as your example `Helper`).</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><br class=""></div><div class="">Thanks and regards,</div><div class="">Muthu</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On 24 Jul 2017, at 4:14 PM, Quinn The Eskimo! via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class="">On 24 Jul 2017, at 07:04, somu subscribe via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">- Is there a bug in my code which is being detected in Xcode 9 ?<br class=""></blockquote><br class="">Yes. The problem here is that `doSomething(f1:)` is a mutating function, so it acts like it takes an `inout` reference to `self.helper`. That’s one mutable reference. It then calls `Car.f1()`, which tries to get a non-mutating reference to exactly the same struct. This is outlawed in Swift 4 as part of the memory ownership effort.<br class=""><br class="">You can read more about the specific change in SE-0176 “Enforce Exclusive Access to Memory”.<br class=""><br class=""><<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md</a>><br class=""><br class="">And the general background to this in the “Ownership Manifesto"<br class=""><br class=""><<a href="https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md" class="">https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md</a>><br class=""><br class=""><blockquote type="cite" class="">If so could you please explain and suggest an alternate approach / fix ?<br class=""></blockquote><br class="">It’s hard to offer concrete suggestions without knowing more about your high-level goals. One option is for `doSomething(f1:)` to pass the `inout` reference through to `f1`. For example:<br class=""><br class=""> mutating func doSomething(f1: (inout Helper) -> ()) {<br class=""> f1(&self)<br class=""> }<br class=""><br class=""> func f1(h: inout Helper) {<br class=""> _ = h.v1 // no crash<br class=""> }<br class=""><br class="">but whether that makes sense in your code is for you to decide.<br class=""><br class="">Share and Enjoy<br class="">--<br class="">Quinn "The Eskimo!" <<a href="http://www.apple.com/developer/" class="">http://www.apple.com/developer/</a>><br class="">Apple Developer Relations, Developer Technical Support, Core OS/Hardware<br class=""><br class=""><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div></blockquote></div><br class=""></body></html>