Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "let" inside Struct - Swift

Tags:

swift

I'm currently practicing examples from Swift Language iBook. My understanding of "let" is that we use "let" to make a constant. Once we assign a value to it, we cannot assign another value to it again. Like the codes below:

let city="NY"
city="LA"  <--error (Cannot assign 'let' value city)

But I saw this example on iBook which really confused me:

struct Color{
let red=0.0, green=0.0, blue=0.0  //<---declare variables using "let" and assign value
init(red:Double,green:Double,blue:Double){
    self.red=red                  //<---assign value to variable again?
    self.green=green               
    self.blue=blue                
    }
}

In this example, it has already assigned values to red, green and blue which use "let".

Why can we assign values to these three variables again in init?

like image 634
ohyes Avatar asked Jun 26 '14 14:06

ohyes


4 Answers

The initialization in the let provides default values if you don't initialize them yourself in the constructor. Constructors (init) are special. Inside them, you can assign to a constant instance variable. In fact, if you don't have a default value for them, you have to assign to them. (This applies to classes too.)

Thanks to Qwerty Bob for finding this in the docs

Modifying Constant Properties During Initialization

You can modify the value of a constant property at any point during initialization, as long as it is set to a definite value by the time initialization finishes.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

like image 156
Kevin Avatar answered Nov 15 '22 08:11

Kevin


You may set constant variables during the init process, before the self keyword is used. After this they are then truly 'constant'.

You must do it before the self keyword is used, as if you pass it to another object it could in turn call a method of yours which relies on that constant property

like image 23
SomeGuy Avatar answered Nov 15 '22 07:11

SomeGuy


And also: structs get passed by value and not by reference, so you cannot modify the variables in a struct at all after their set. So the let keyword really makes sense.

like image 33
Kametrixom Avatar answered Nov 15 '22 07:11

Kametrixom


If you continue reading a few paragraphs after the example you gave from the book (unless they use it in multiple locations), it actually talks about this behavior:

You can modify the value of a constant property at any point during initialization, as long as it is set to a definite value by the time initialization finishes.

So basically you can modify constants and upon ending the initialization all constants must have a definitive value. It also goes on to talk about how this works with subclasses too:

For class instances, a constant property can only be modified during initialization by the class that introduces it. It cannot be modified by a subclass.

Here is the doc reference for it (same as the book), the quoted sections is under the "Modifying Constant Properties During Initialization" subheading.

like image 23
Firo Avatar answered Nov 15 '22 07:11

Firo