<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 dir="auto" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">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=""><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="">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" style="font-size: 11px;" class="">Helper</font> to a <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">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><br class=""></div><div>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 <i class="">object aliasing</i> before deciding which approach is the right approach for you. </div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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 style="font-size: 11px;" class="">C1</span></font>, <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">C2</span></font>, <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">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 style="font-size: 11px;" class="">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 style="font-size: 11px;" class="">class</span></font> / <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">struct</span></font> which would contain the common code. I can make the helper a <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">private property</span></font> so that the functions wouldn’t be exposed to the instances of <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">C1</span></font>, <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">C2</span></font>, <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">C3</span></font>. In order to achieve that I had to pass some functions from <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">C1</span></font> into the <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">Helper</span></font> <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">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 style="font-size: 11px;" class="">Helper class</span></font> (or <font color="#0433ff" face="Menlo" class=""><span style="font-size: 11px;" class="">struct</span></font>) or is there a more better approach ?</div></div></div></blockquote><div><br class=""></div><div>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><br class=""></div><div>Generally, what you call `Helper` is the correct way to go if the following is true: </div><div><br class=""></div><div>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><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>