<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Apr 17, 2017 at 4:52 PM, Tino Heth <span dir="ltr">&lt;<a href="mailto:2th@gmx.de" target="_blank">2th@gmx.de</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><span class="gmail-"><div><br><blockquote type="cite"><div><span 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;float:none;display:inline">the only way to hide one invariant from the other’s methods is to use helper types</span></div></blockquote></div></span>Could you elaborate here and give an example that uses such helper types?</div></blockquote></div><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Okay, let’s say we have a type Foo with a String property and an Int property. The invariants we’ll preserve are that the String must begin with “secret ” and the Int must be divisible by 3.</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra"><font face="monospace, monospace">struct Foo {</font></div><div class="gmail_extra"><font face="monospace, monospace">    private var secretString = SecretString()</font></div><div class="gmail_extra"><font face="monospace, monospace">    private var divisibleBy3 = MultipleOfThree()</font></div><div class="gmail_extra"><font face="monospace, monospace">    </font></div><div class="gmail_extra"><font face="monospace, monospace">    var x: Int {</font></div><div class="gmail_extra"><font face="monospace, monospace">        get { return divisibleBy3.value }</font></div><div class="gmail_extra"><font face="monospace, monospace">        set { divisibleBy3.update(newValue) }</font></div><div class="gmail_extra"><font face="monospace, monospace">    }</font></div><div class="gmail_extra"><font face="monospace, monospace">    </font></div><div class="gmail_extra"><font face="monospace, monospace">    var y: String {</font></div><div class="gmail_extra"><font face="monospace, monospace">        get { return secretString.value }</font></div><div class="gmail_extra"><font face="monospace, monospace">        set { secretString.update(newValue) }</font></div><div class="gmail_extra"><font face="monospace, monospace">    }</font></div><div class="gmail_extra"><font face="monospace, monospace">    </font></div><div class="gmail_extra"><font face="monospace, monospace">    // rest of the implementation</font></div><div class="gmail_extra"><font face="monospace, monospace">}</font></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">private struct MultipleOfThree {</font></div><div class="gmail_extra"><font face="monospace, monospace">    private(set) var value: Int = 0</font></div><div class="gmail_extra"><font face="monospace, monospace">    mutating func update(_ n: Int) { value = 3 * n }</font></div><div class="gmail_extra"><font face="monospace, monospace">}</font></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">private struct SecretString {</font></div><div class="gmail_extra"><font face="monospace, monospace">    private(set) var value: String = &quot;secret value&quot;</font></div><div class="gmail_extra"><font face="monospace, monospace">    mutating func update(_ s: String) { value = &quot;secret &quot; + s }</font></div><div class="gmail_extra"><font face="monospace, monospace">}</font></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">Now the only places that the invariants could possibly be broken are inside the helper types themselves. Anything within the implementation of Foo can call the “update” functions, but cannot directly alter the values inside the helper types and so cannot violate the invariants. Thus, with this pattern, to confirm that the invariants are preserved we need only look at the helper types, which are short and self-contained.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Nevin</div></div>