<div dir="ltr">Here's the one I started with:<div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"> </span><span style="font-size:13px">lazy var c:Int = {return a*b}()</span><br></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">and ended up with:</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"><div> lazy var c:Int = {return self.a * self.b}()</div><div><br></div><div>It's in a closure, so need to explicitly reference self.</div><div><br></div><div>Cheers,</div><div>++md</div><div><br></div></span></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 4, 2016 at 3:04 PM, J.E. Schotsman via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On 04 Jul 2016, at 19:21, Zhao Xin <<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>> wrote:<br>
><br>
> You'd better sharing some of you code here first.<br>
<br>
</span>For example, consider this:<br>
<br>
class TestStruct1<br>
{<br>
let a = 10<br>
let b = 20<br>
let c:Int = {return self.a*self.b}()<br>
}<br>
<br>
Of course this is a trivial example. In reality the calculation of c from a and b might take longer.<br>
<br>
Since this is not allowed I try<br>
<br>
struct TestStruct2<br>
{<br>
let a = 10<br>
let b = 20<br>
lazy var c:Int = {return a*b}()<br>
}<br>
<br>
Not allowed either even though neither a nor b is lazy.<br>
I have to do<br>
<br>
struct TestStruct3<br>
{<br>
let a = 10<br>
let b = 20<br>
<br>
private var cInitialized = false<br>
private var _c = 0<br>
var c:Int<br>
{<br>
mutating get {<br>
if !cInitialized<br>
{<br>
_c = a*b<br>
cInitialized = true<br>
}<br>
return _c }<br>
}<br>
}<br>
<br>
BTW I pasted Mark’s code in a playground and it compiles indeed.<br>
What’s the difference?<br>
<div class="HOEnZb"><div class="h5"><br>
Jan E.<br>
<br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>