[swift-users] Setting up properties

Joe Groff jgroff at apple.com
Mon Dec 21 20:47:53 CST 2015


> On Dec 21, 2015, at 5:14 PM, Erica Sadun via swift-users <swift-users at swift.org> wrote:
> 
> For a friend:
> 
> This won't work.
> 
> class DerpController {
>     private(set) var derp: Bool =  {
>         return _findDerp() != nil
>     }()
>     
>     private func _findDerp() -> AnyObject? {
>         return nil
>     }
> }
> 
> 
> but this works:
> 
> private func _findDerp() -> AnyObject? {
>     return nil
> }
> 
> class DerpController {
>     private(set) var derp: Bool =  {
>         return _findDerp() != nil
>     }()
>     
> }
> 
> Is it because the var member cannot depend on another member during setup? Thanks in advance for insight.
> 

Yeah, instance methods can't be called until `self` is fully initialized, since they may themselves touch the uninitialized instance. In addition to making the function global, you could also make it a static method to preserve namespacing.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151221/74de2ddc/attachment.html>


More information about the swift-users mailing list