I believe variables under private(set) can still be accessed with KVC (At least when the class inherits from nsobject.)<br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 22, 2016 at 9:19 AM Andy Chou via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">Point taken. I think Swift supports both styles reasonably well. Initially it looked like writing the 'with' function would be troublesome but it turned out to be quite simple.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I appreciate the point you're making, which is that you get much of the value of purely immutable values with Swift's mutable structures + value semantics + let constant structs. And, you also get a simpler implementation in some cases (e.g. not needing functions like 'with').</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Swift has a pretty interesting model here that seems to capture some of the best parts of the imperative and immutable/functional styles. For example, I had to check whether this worked as expected:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">```</div><div class="gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">struct Person {<br class="gmail_msg"> var name: String<br class="gmail_msg"></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"> var address: String<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg">class C {<br class="gmail_msg"> private(set) var currentPerson = Person(name: "Andy", address: "1 Battery St")<br class="gmail_msg">}<br class="gmail_msg"><br class="gmail_msg">func f() {<br class="gmail_msg"> let instance = C()<br class="gmail_msg"> <a href="http://instance.currentPerson.name" class="gmail_msg" target="_blank">instance.currentPerson.name</a> = "Dave" // Cannot assign to property: 'currentPerson' setter is inaccessible<br class="gmail_msg">}</div><div class="gmail_msg">```<br class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">This means the implementation of class C can set the currentPerson and its properties, but the currentPerson is immutable to the outside. A similar thing could be accomplished with an immutable Person, but you still need to use private(set), so it's no more complicated or risky to use the mutable Person.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">The difference is mostly one of style, way of thinking, and psychology. For some people seeing the declaration of Person having vars is uncomfortable if they are used to a functional style. "If I have a Person, will it be changed unexpectedly out from under me?" For people who know Swift well, the fact that it's a struct means any potential change to Person can only happen under controlled circumstances like an inout parameter, and only if I declare it a var. This discussion was very helpful for me to understand the nuances of struct better.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Thanks,</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Andy</div></div><div style="word-wrap:break-word" class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On Dec 22, 2016, at 7:01 AM, Matthew Johnson <<a href="mailto:matthew@anandabits.com" class="gmail_msg" target="_blank">matthew@anandabits.com</a>> wrote:</div><br class="m_8750169531233929282Apple-interchange-newline gmail_msg"><div class="gmail_msg"><div style="font-family:Helvetica;font-size:14px;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" class="gmail_msg">Yes, this is true. However, this thread is about improving the convenience of immutable structs / functional code. It is quite common in such code that the previous value is no longer needed. I am encouraging people to focus on what the real benefits of this style is (value semantics) and how the same benefit might be achieved in a different way in a hybrid language like Swift.</div></div></blockquote></div><br class="gmail_msg"></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>