<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="">No, newValue is a closure here, and valueSource will capture this closure. When valueSource is evaluated, the original closure will be evaluated.<div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Sep 11, 2017, at 3:11 PM, Hooman Mehr <<a href="mailto:hooman@mac.com" class="">hooman@mac.com</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="">But the expression that is assigned to the property will be eagerly evaluated to produce `newValue`. So this will not accomplish what Nevin is trying to do. </div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 11, 2017, at 3:08 PM, Slava Pestov 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="">You can have valueSource store a closure that captures the autoclosure value. For example,<div class=""><br class=""></div><div class="">set {</div><div class=""> valueSource = { newValue }</div><div class="">}</div><div class=""><br class=""></div><div class="">Slava</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 11, 2017, at 11:04 AM, Nevin Brackett-Rozinsky 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 dir="ltr" class="">Hi, quick question here:<div class=""><br class=""></div><div class="">I have a class with a property that needs to be really *really* lazy. So lazy, in fact, that when you assign to that property, the class actually stores a closure of what you assigned, which is only evaluated if and when you actually attempt to read the property.</div><div class=""><br class=""></div><div class="">Simplified:</div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">class Foo {</font></div><div class=""><font face="monospace, monospace" class=""> private var valueSource: () -> Bar</font></div><div class=""><font face="monospace, monospace" class=""> private var valueCache: Bar?</font></div><div class=""><font face="monospace, monospace" class=""> </font></div><div class=""><font face="monospace, monospace" class=""> init(_ v: @escaping @autoclosure () -> Bar) {</font></div><div class=""><font face="monospace, monospace" class=""> valueSource = v</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class=""> </font></div><div class=""><font face="monospace, monospace" class=""> var value: Bar {<br class=""></font></div><div class=""><font face="monospace, monospace" class=""> get {</font></div><div class=""><div class=""><span style="font-family:monospace,monospace" class=""> if let v = valueCache { return v }</span><font face="monospace, monospace" class=""><br class="gmail-Apple-interchange-newline"></font></div><div class=""><span style="font-family:monospace,monospace" class=""> let w = valueSource()</span><span style="font-family:monospace,monospace" class=""><br class=""></span></div><div class=""><span style="font-family:monospace,monospace" class=""> valueCache = w</span><span style="font-family:monospace,monospace" class=""><br class=""></span></div><div class=""><span style="font-family:monospace,monospace" class=""> return w</span></div></div><div class=""><span style="font-family:monospace,monospace" class=""> }</span><br class=""></div><div class=""><font face="monospace, monospace" class=""> </font><span style="font-family:monospace,monospace" class="">set {</span></div><div class=""><font face="monospace, monospace" class=""> /* ??? */</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class=""> </font></div><div class=""><font face="monospace, monospace" class=""> // I want this function's logic to go in the setter above</font></div><div class=""><font face="monospace, monospace" class=""> func setValue(_ v: @escaping @autoclosure () -> Bar) {</font></div><div class=""><font face="monospace, monospace" class=""> valueSource = v</font></div><div class=""><font face="monospace, monospace" class=""> valueCache = nil</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div><div class=""><br class=""></div><div class="">The goal is to be able to write things like “someFoo.value = bar1 / bar2” (or even more complex expressions) and not evaluate them until/unless the result is actually needed.</div><div class=""><br class=""></div><div class="">Currently I am using “someFoo.setValue( bar1 / bar2 )”, which is not nearly as ergonomic as the assignment syntax. So, is there a way to make this work?</div><div class=""><br class=""></div><div class="">Nevin</div></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><br class=""></div></blockquote></div><br class=""></div></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><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></body></html>