Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you use an optional type for a constant with value?

Have a look at this statement from Apple document:

let optionalInt: Int? = 9

Why would you use Int? as the type for this constant? You know that it cannot be nil as you are assigning the value 9 to it? The purpose of an optional type (as I understand) is to be able to hold nil. There is no need for optionalInt to hold nil in this statement. Could someone explain?

like image 605
Andrew F. Avatar asked Mar 03 '16 14:03

Andrew F.


People also ask

Can constants 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.

What is an optional value?

An optional value either contains a value or contains nil to indicate that the value is missing.

Why do we need optional in Swift?

Swift's optionals are one of its most powerful features, while also being one of the most confusing. Their core job is simple: they allow us to represent the absence of some data – a string that isn't just empty, but literally doesn't exist.

How does Swift handle optional value?

In Swift, optionals are nil or have a value. Before you can use an optional, you'll need to unwrap it. Optionals are a powerful feature of the Swift programming language, because they make your code safer and more productive.


3 Answers

I found a case where you might use it. It's a bit of a stretch, but here we go.

One reason for using let x: Int? = whatever is if you have an overloaded function where the parameters are different types only insofar as one is an optional. For example:

func doSomething(x: Int)
{
    print("\(x) is an Int")
}

func doSomething(x: Int?)
{
    print("\(x) is an optional")
}

If you want to ensure the second overload is called, you need to make the parameter explicitly an optional e.g.

let y = 5
doSomething(y) // prints 5 is an Int

let z: Int? = 6
doSomething(z) // prints Optional(6) is an optional

I've yet to see anything like this in real life.

As regards the Apple documentation, it's just a simple example.

like image 194
JeremyP Avatar answered Sep 24 '22 12:09

JeremyP


I think this particular part of the Apples document is intended to simply illustrate the concept of optionals and nothing else. They probably use let out of the good practice to always start with declaring let properties/variables and only switch them to var if there is an explicit need to change their values.

As to the question why would one declare let foo: Int? = 9 and not just let foo = 9. Well, I really can not imagine where it would be needed. Even if some API expects an Int? then you still can pass a variable of Int type to such API and Swift will implicitly convert it for you. Perhaps, if you pass it to a generic function and you want the Int? version of that generic to be used then you will want to do that, but it's way too academic already to have practical use.

like image 23
0x416e746f6e Avatar answered Sep 22 '22 12:09

0x416e746f6e


A couple ideas:

Assuming you have a method

func foo(xValue: Int?)->Int? {
  // some logic
}

Perhaps you want to define your constant to exactly match the method definition.

let optionalInt: Int? = 9
let fooVal = foo(optionalInt)

Or (changing your initial let statement), the constant's value is determined by a function that returns an Optional

let optionalInt: Int? = foo(nil)
like image 26
AgRizzo Avatar answered Sep 23 '22 12:09

AgRizzo