user7024499 user7024499 - 17 days ago 5
Swift Question

What's the point of READ-only variables when you have LET?

For example:

var dogName : String {
return "Buster"
}


VS..

let dogName = "Buster"


Let's say we're declaring each of these at the top level of a class as instance properties. Are these just two ways of doing the same thing? If not, what's the point of having a read-only variable?

Thanks

Answer

Let me try to sum up what the other answers are saying while also adding missing information that I think is critical in order to understand this.

Properties

Properties are simply values that are associated with an object and may be queried in a trivial amount of time without the need (or ability) for parameters like methods have.

Stored Properties

When you create a stored property, whether with let or var, the value assigned at any given point in time will be stored in memory, which is why it is called a stored property.

var name = "Matt"

For variables using var, the value is stored in memory in a way that makes it mutable (editable). You can reassign the value at will and it will replace the previous value stored in memory.

let name = "Matt"

For constants using let, the value is also stored in memory, but in such a way that it may not be changed after the first time assigning to it.

Computed Properties

Computed properties are not stored in memory. As ganzogo says in the comments, computed properties act similarly to methods, but do not take parameters. When deciding when to use a computed property or a function with no parameters, the Swift API Design Guidelines recommend using a computed property when it will simply create or fetch, and then return the value, provided that this takes a trivial amount of time.

var fullName: String {
    return firstName + lastName
}

Here, we assume that firstName and lastName are already properties on the object. There is no sense of initialization with this property because it is not stored anywhere. It is fetched on demand every time. That is why there is no sense to doing anything like the following:

var dogName : String {
    return "Buster"
}

This has no benefit over a stored property except that no memory will be used in storing the String "Buster".

In fact, this is a simplified version of computed properties. You will notice that the Swift Language Guide describes the use of both get and set in a computed property. set allows you to update the state of other variables when one sets a computed variable. For example:

var stored: Int

var computed: Int {

    get {
        return stored + 5
    }

    set {
        stored = newValue - 5
    }

}

Some useful applications of this were pointed out by Rajan's answer, for example getting and setting volume from width, height, and depth.

A read-only computed var is just a computed var which specifies only a getter, in which case the get keyword and brackets are not required.

Read-Only for Access Control

When developing modules such as frameworks, it is often useful to have a variable only be modifiable from within that object or framework and have it be read-only to the public.

private var modifiableItem: String

public var item: String {
    return modifiableItem
}

The idea here is that modifiableItem should only be mutable from within the object that defined it. The private keyword ensures that it is only accessible within the scope of the object that created it and making it a var ensures that it may be modified. The public var item, then, is a computed variable that is exposed to the public that enables anyone to read, but not mutate the variable.

Comments