2

A note on page 248 in The Swift Programming Language (Swift 2.1) explains the following:

Global constants and variables are always computed lazily, in a similar manner to Lazy Stored Properties. Unlike lazy stored properties, global constants and variables do not need to be marked with the lazy modifier.

Local constants and variables are never computed lazily.

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.1).” https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

Is there another type of constant or variable besides global properties and local values where the lazy modifier would have an affect?

Community
  • 1
  • 1
grossmae
  • 331
  • 5
  • 16

2 Answers2

2

"Local constants and variables" in that provided excerpt refer to local scope constants and variables, as in local variables of a function. They do not refer to properties of objects, which can be lazy, if they are marked with the lazy keyword.

//global, declared outside of a class/struct
//error is "Lazy is only valid for members of a struct or class
lazy var label: UILabel = {
    var tempLabel: UILabel = UILabel()
    tempLabel.text = "hi"
    return tempLabel
}()

class SomeClass : NSObject {
    //non-lazy instance property
    var x = 3

    //lazy instance property
    lazy var label: UILabel = {
        var tempLabel: UILabel = UILabel()
        tempLabel.text = "hi"
        return tempLabel
    }()


    func doStuff() {
        //error is "Lazy is only valid for members of a struct or class
        lazy var label: UILabel = {
            var tempLabel: UILabel = UILabel()
            tempLabel.text = "hi"
            return tempLabel
        }()
    }
}
Will M.
  • 1,864
  • 17
  • 28
  • What is the difference between properties of objects and "global constants and variables"? – grossmae Mar 16 '16 at 18:01
  • @grossmae does my edit with examples clear up the difference? – Will M. Mar 16 '16 at 18:12
  • 1
    @grossmae Sorry, I was editing for clarity still. You can have globals, declared outside of your class, which are always lazy. You can have statics on your class, which are always lazy, you can have local variables inside your methods, which are never lazy and can't be, and you can have instance properties which can be lazy or not. – Will M. Mar 16 '16 at 18:18
  • How are globals lazy? If I do `let removedElement = myArray.remove(at: 0)`, it's being removed immediately, not waiting until I use `removedElement` anywhere else. I don't understand what is this global laziness about. – Robo Robok Jul 06 '17 at 21:55
  • @RoboRobok my source is [this, same as OP linked:](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-ID263) under Global and Local Variables, it says "NOTE Global constants and variables are always computed lazily, in a similar manner to Lazy Stored Properties." I confess I didn't really investigate it further once finding that in the Apple docs. If you are using playground, what you see could just be a side-effect of how playgrounds work. – Will M. Jul 07 '17 at 15:18
0

Global here refers to the variable and constants declared outside of the class parenthesis { } for example

var anotherProerty : AClass() //This will be lazy by default 

Class Abc 
    {
        var aProperty : AClass()  // This will NOT be lazy by default
        //however it can be lazy by declaring it with 'lazy' KEYWORD

        func abc()
        {
            var another: AClass()  //This cannot be lazy 
        }


    }
Asadullah Ali
  • 1,056
  • 14
  • 31