<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br><br>Sent from my iPad</div><div><br>On May 18, 2016, at 8:17 AM, Haravikk via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">On 18 May 2016, at 10:08, David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class="">`lazy` properties can't be `let`. Personally, I disagree with this — their initial mutation is transparent, and it's conceptually perfectly valid to have a value that's, say, too expensive to calculate during every instance's init and whose value never changes once calculated — but there may well be Very Good Reasons for needing them to be `var`.</div></div></div></blockquote><div><br class=""></div><div>You can kind of do this already by adding private(set) to a lazy property, but yeah, the ability to define it as let would be more concise, work within the same type/file and possibly be more intuitive as well. Really it’s just an indicator to the compiler that a value shouldn’t be mutated so it can stop you, though there are some optimisations that can be done as well, many of these may not apply to lazy properties due to the way that they work.</div></div></div></blockquote><div><br></div><div>Yep, there are two parts to this. &nbsp;One is that the current 'let' model is extremely strict and thus excludes lazy because it is written to after the instance is initialized. &nbsp;Following from this is the assumptions it allows the optimizer to make.</div><div><br></div><div>We discussed relaxing 'let' rules in the memberwise init discussion thread. &nbsp;The core team seems to be willing to consider this with respect to the issue that came up there. &nbsp;</div><div><br></div><div>I think a similar case could be made for lazy let, and more generally distinguishing the programmer model of immutability from the optimizer model which may need to rely on immutability of the underlying bits themselves. &nbsp;This distinction would allow the programmer model to be modified to allow for lazy let. &nbsp;</div><div><br></div><div>I think it is worth considering (after Swift 3) refining the programmer model to mean the value is always initialized before reading and is never written to after initialization. &nbsp;This allows for implementation flexibility while preserving the semantics visible to users of the type. &nbsp;In that respect it I believe it is similar in spirit to allowing internal references and CoW in the implementation of value semantic types.</div><div><br></div><div>It's also worth noting that lazy let is similar to a memoized computed let so it may help address that use case as well. &nbsp;The difference is that with a computed let the compiler would need to guarantee the implementation is a pure function on immutable values.</div><div><br></div><div>One related area is weak properties. &nbsp;I believe there is a strong case for something in between 'let' and 'private(set) var' here. &nbsp;It may be desirable to guarantee that your code only initializes the weak property. &nbsp;The only desired value change is when it is zeroed. &nbsp;I'm not sure what this would look like, but maybe 'invisible(set) var' which guarantees that only the Swift runtime will modify the value after initialization.</div><div><br></div><blockquote type="cite"><div><div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="auto" class=""><div class=""><div class="">On May 17, 2016, at 17:23, Leonardo Pessoa &lt;<a href="mailto:me@lmpessoa.com" class="">me@lmpessoa.com</a>&gt; wrote:</div><br class=""></div><blockquote type="cite" class=""></blockquote></div></div></blockquote><div class=""><div dir="auto" class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">David, I'm thinking about the side effects calling a computed property has and although I see some use cases for let properties I also see workarounds. For example, a lazy initialiser will solve the issue of running a certain code only once and caching its value. I also start to think that any case in this proposal will be solved by lazy initialisers thus rendering it unnecessary.</div></div></div></blockquote></div></div><br class=""></div><div>I’m not sure that an immutable lazy property and an immutable computed property are mutually exclusive. I gave an example earlier, though it was based on a misunderstanding of the proposal, but it seems relevant. Imagine if you have a type that has two large immutable array properties, and a computed property that combines them in some way. Do you really want to double the size of your instance by storing that computed value, even after it is no longer being referenced elsewhere? If each array were 512kb, you’d be bloating the type to 2mb to store a value that may never be used again, which is one of the main drawbacks of lazy properties; you could maybe work around this with weak references or something, but that just adds more complexity, whereas a computed property solves the problem by leaving it up to whoever called it to store the value if it makes sense to do-so.</div><div><br class=""></div><div>So yeah, I think that both features have their uses, and while computed “let” properties may be relatively niche, the biggest benefit of them is really that they self-advertise that they are unchanging, and that the compiler can help to ensure this is the case, so it’s both a feature for API users to take advantage of, and a defensive feature to help you to code your property.</div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>