[swift-users] lazy initialisation

Zhao Xin owenzx at gmail.com
Mon Jul 4 20:47:17 CDT 2016


No, it is not a bug.

For a closure, you have to call self explicitly unless the closure is mark
as @noescape. Also, in this situation, self is not unowned, as the closure
is not stored, it ran and released. Below, is a situation that you need use
unowned self. Here the closure is stored in variable d instead of running
and releasing.

    lazy var d:()->Int = { [unowned self] in
        return self.a*self.b
    }

Zhaoxin

On Tue, Jul 5, 2016 at 3:42 AM, Karl via swift-users <swift-users at swift.org>
wrote:

>
> On 4 Jul 2016, at 21:12, Mark Dalrymple via swift-users <
> swift-users at swift.org> wrote:
>
> Here's the one I started with:
>
>    lazy var c:Int = {return a*b}()
>
> and ended up with:
>
>     lazy var c:Int = {return self.a * self.b}()
>
> It's in a closure, so need to explicitly reference self.
>
> Cheers,
> ++md
>
>
>
> I’ve been hitting this myself. I think it’s a bug -- yes, you are
> technically in a closure, but the closure does not capture ‘self’ beyond
> the lifetime of ‘self’. In this context, ’self’ should automatically be
> ‘unowned’.
>
> Karl
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160705/754e199f/attachment.html>


More information about the swift-users mailing list