Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why optional constant does not automatically have a default value of nil [duplicate]

Tags:

swift

optional

The following code works fine

struct carConfi {
    var owner: String?
    let brand: String = "BMW"
    var currentMile: Double = 2000
}

let tomCar = carConfi()

However, if I change the type of the property owner to constant, there will be an error at the initializer

struct carConfi {
        let owner: String? // Change to constant
        let brand: String = "BMW"
        var currentMile: Double = 2000
    }

let tomCar = carConfi() //error: missing argument for parameter 'owner' in call 

I did a bit search, it turns out that it is because the optional variables automatically have a default value of nil

I guess: Because once the constant is set, it then cannot be changed, if the optional constant automatically received an nil then it will keep as an unchangeable nil that's very silly and may against the users will

Question: My college doesn't fully convinced by the guess, he told me there must be more reasons for that. I would very appreciate if someone can explain that to me

Thx

like image 897
SLN Avatar asked May 23 '16 20:05

SLN


People also ask

How do you know if optional is nil?

In Swift, if we define a variable to be an optional variable, in that case, this variable can have no value at all. If optional variable is assigned with nil , then this says that there is no value in this variable. To check if this variable is not nil, we can use Swift Inequality Operator !=

Can a constant be optional in Swift?

However there is another data type in Swift called Optional, whose default value is a null value ( nil ). You can use optional when you want a variable or constant contain no value in it. An optional type may contain a value or absent a value (a null value).

What is the difference between optional none and nil?

Question : What's the difference optional between nil and . None? Answer : There is no difference.

Which Has a default value of null?

If no default value is declared explicitly, the default value is the null value. This usually makes sense because a null value can be considered to represent unknown data. In a table definition, default values are listed after the column data type.


2 Answers

Not setting a read-only (constant) field with either an:

  • initialization expression
  • initializer

is almost certainly an indication of an error in your program.

Since you have no other opportunity to set the value of your let field, the value of the field is going to remain nil (or some other default). It is rather unlikely that a programmer would find such behavior desirable, and request it on purpose.

That is why Swift marks this situation as an error. On the other hand, if you actually wanted your String constant to remain nil, you could add an expression to set it to nil, and silence the error:

let owner: String? = nil // Pretty useless, but allowed
like image 153
Sergey Kalinichenko Avatar answered Oct 12 '22 07:10

Sergey Kalinichenko


Constants are set once, and once only. If you wanted it to be null or 0, then you would set it. You always define a constant on initiation.

like image 42
JohnV Avatar answered Oct 12 '22 05:10

JohnV