<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 14, 2016, at 17:16, Dave via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@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="">I might play with it a bit tonight after dinner.</div></div></div></blockquote></div><br class=""><div class="">Ok, so here’s what I came up with. It <i class="">partially</i> works. Also, I thought it might be easier to supply a list of behaviors instead of using function composition. Both ways are included, since I’d already written the one using function composition when I thought of the other way.</div><div class=""><br class=""></div><div class="">Oh, and to be clear, I’m NOT saying this is the way I think we should do this… I’m just exploring how for we can take it without adding any new language features.</div><div class=""><br class=""></div><div class="">Here’s the “library” part:</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">//: Playground - noun: a place where people can play</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">import</span> Cocoa</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span> Wrapper {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>typealias<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> T</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span> {<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span>}</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// "assignment" operator, since we can't overload the real one</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// (yes, I know "≠" means "not equals"... it's easy to type, and</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// this is just for a proof of concept)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">infix<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>operator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> ≠ {}</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> ≠ <U: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wrapper</span>, V: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wrapper</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span> == <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>> (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inout</span> lhs: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>, rhs: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>) { lhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span> = rhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> ≠ <V: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wrapper</span>> (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inout</span> lhs: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, rhs: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>) { lhs = rhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> ≠ <V: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wrapper</span>> (<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inout</span> lhs: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>, rhs: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) { lhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span> = rhs }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// function composition. Nothing special about "•"... it's just about as close as</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// I could get to the "⊙" my math textbooks used without pulling up the symbols palette</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">infix<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>operator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> • {}</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> • <T, U, V> (f1: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span> -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>, f2: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span> -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>) -> (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span> -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>) { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> { f1(f2($0)) } }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// function currying. Nothing special about "<|>"... is was just what someone else used</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">infix<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>operator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> <|> {</span>precedence<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">200</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">}</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> <|> <T, U, V> (f: (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>)-><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>, b: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">U</span>) -> (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>-><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">V</span>) { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> { f($0, b) } }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// Seemed the easiest way to handle “lazy”.</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">enum</span> MaybeLazy<T> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> Yep(()->T)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> Nah(T)</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> value: ()-><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Yep</span>(value) }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Nah</span>(value) }</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> .Nah(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> t): <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> t</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> .Yep(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> tc): <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> tc()</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Nah</span>(newValue)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// Where the sufficiently advanced technology happens</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Observable<T> : </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Wrapper</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span>CustomStringConvertible<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// I'm not actually sure this typealias needs to exist. Sometimes</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// the playground gets... stubborn... about what is or isn't an error,</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// and there's no way to clean/build. Plus I lose track of where the Ts go.</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typealias</span> FType = (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>)->(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>)</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// The "do nothing" closure</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> _passThrough(x:(oldValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>)) -> (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> x }</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// "Function"</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> f: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">FType</span>! = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// "Element Function". More on this later.</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> ef: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Any</span>? = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> _value:<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MaybeLazy</span><<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span> = (<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> != <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span>) ? <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span>, newValue).<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> : newValue }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// inits for function composition</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> f: (oldValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>)->(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">_passThrough</span>) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Nah</span>(value)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = f</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(lazy: ()-><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> f: (oldValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>)->(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">_passThrough</span>) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Yep</span>(lazy)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = f</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// inits for variadic list of functions</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, behaviors: ((oldValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>)->(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>))...) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Nah</span>(value)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span> behaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">_passThrough</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = behaviors[<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>]</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span>:</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> c = behaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">last</span>!</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> i <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> ..< (behaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span> - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>)).<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">reverse</span>() {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> c = c <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">•</span> behaviors[i]</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = c</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(lazy: ()-><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, behaviors: ((oldValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>)->(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>))...) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Yep</span>(lazy)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span> behaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">_passThrough</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = behaviors[<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>]</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span>:</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> c = behaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">last</span>!</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> i <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> ..< (behaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span> - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>)).<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">reverse</span>() {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> c = c <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">•</span> behaviors[i]</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span> = c</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> description: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"</span>\<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">// the behavior functions to be passed in</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> didSet <T> (x: (oldValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>), <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> f: ((<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>))->((<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>))) -> (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> f(x)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> (x)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> didChange<T: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Equatable</span>> (x: (oldValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, newValue: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>), <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> f: ((<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>))->((<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>))) -> (<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> x.oldValue != x.newValue {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> f(x)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> (x)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div></blockquote><div class=""><br class=""></div>And here’s how you’d use it (except with “=“ instead of “≠”, of course):<blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> exampleOfCompositionSyntax = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>,</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>didSet<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><|><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"didSet"</span>)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> ($0, $1)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> } </span>•<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>didChange<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><|><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"didChange"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> ($0,$1)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">exampleOfCompositionSyntax</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">≠</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// the closure passed to didSet is called, but not didChange</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">exampleOfCompositionSyntax</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">≠</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// both are called, and it now evaluates to 4</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> exampleOfVariadicSyntax = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span>, behaviors:</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>didSet<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><|><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"didSet"</span>)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> ($0, $1)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> },</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>didChange<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><|><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"didChange"<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> ($0,$1)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">exampleOfVariadicSyntax</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">≠</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// the closure passed to didSet is called, but not didChange</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">exampleOfVariadicSyntax</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">≠</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// both are called, and it now evaluates to 4</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> nowWithMoreLaziness = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>(lazy: {<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">4</span>})</div></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">var</span><font face="Menlo" class=""><span style="font-size: 11px;" class=""> ibar = </span></font><span style="color: rgb(39, 42, 216); font-family: Menlo; font-size: 11px;" class="">0</span><span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 11px;" class=""> </span><font color="#008400" face="Menlo" class=""><span style="font-size: 11px;" class="">// here’s one glitchy bit… ibar has to be declared first since “=“ can’t be overloaded</span></font></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class="">ibar<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">≠</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>nowWithMoreLaziness<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// ibar evaluates to 4</span></div></div></blockquote><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class=""><br class=""></span></div><div class="">Trying to extending the behavior to collections is where things kinda fall apart. This code doesn’t generate any errors, but it causes Xcode to repeatedly “lose communication with the playground service":</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> T: MutableCollectionType {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// This extension is where we support per-element behavior. "ef" is really</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// of type "CType", but we couldn't declare it that way because we didn't</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// know that T was a MutableCollectionType until now.</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typealias</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> CType = (</span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Index<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)->(</span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">,</span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Index<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>// The "do nothing" closure</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> _ePassThrough(x:<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CType</span>) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CType</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> x }</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> value: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>, elementalbehaviors: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CType</span>...) {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">_value</span> = .<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">Nah</span>(value)</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span> elementalbehaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span> {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ef</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Observable</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">_ePassThrough</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ef</span> = elementalbehaviors[<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span>]</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span>:</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> c = elementalbehaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">last</span>!</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> i <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span> ..< (elementalbehaviors.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">count</span> - <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>)).<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">reverse</span>() {</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> c = c <span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">•</span> elementalbehaviors[i]</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ef</span> = c</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(i: </span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Index<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) -> </span>T<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">get</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span>[i] }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">set</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span>[i] = (<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ef</span> != <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span>) ? (<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ef</span>! <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span>! <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CType</span>)(<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">value</span>[i], newValue, i).<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> : newValue }</div></div><div class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> </p></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div></blockquote><div class=""><br class=""></div><div class=""><div class="">So is it a bug in my code, or a bug in Playgrounds that’s causing the problem? Dunno, and I’m too tired to find out tonight. Obviously, if any of you want to play with it, go ahead… that’s why I posted the code :-)</div></div><div class=""><br class=""></div><div class=""><div class="">- Dave Sweeris</div></div><div class=""><br class=""></div></body></html>