Does "let _ = ..." (let the underscore be equal) be used in Swift?

Does using let _ = ... have any purpose?

I have seen the question and answers. What is the underscore representative in Swift References? , and I know that underscores can be used to represent a variable that is not needed .

This would make sense if I needed only one tuple value, as in the example from the link above:

 let (result, _) = someFunctionThatReturnsATuple() 

However, I recently met this code :

 do { let _ = try DB.run( table.create(ifNotExists: true) {t in t.column(teamId, primaryKey: true) t.column(city) t.column(nickName) t.column(abbreviation) }) } catch _ { // Error throw if table already exists } 

I don't get any warnings or compiler errors if I just delete let _ = . It seems to me that it is easier and more understandable.

 try DB.run( table.create(ifNotExists: true) {t in t.column(teamId, primaryKey: true) t.column(city) t.column(nickName) t.column(abbreviation) }) 

The author of the code wrote a book and blog about Swift . I know that the authors are not infallible, but it made me wonder if there is something that I do not see.

+8
source share
5 answers

You will get a compiler warning if the method was noted warn_unused_result from the developer documentation :

Apply this attribute to the declaration of a method or function, so that the compiler generates a warning when a method or function is called without using its result.

This attribute can be used to provide a warning message about the incorrect use of a method without a mutation that has a mutating copy.

+8
source

Using let _ = ... specifically tells the compiler that you know that the expression on the right returns a value, but you don't care.

In cases where the method was marked warn_unused_result , if you do not use the underscore, then the compiler will complain about the warning. (Because in some cases it may be a mistake not to use the return value.)

+6
source

try? it sometimes easier and cleaner to use try? than do-catch when you call something that throws but decide not to handle any errors. Should you leave a call with try? as-is, the compiler will warn you about an unused result, which is not very good. This way you can undo the results with _ .

Example:

 let _ = try? NSFileManager.defaultManager().moveItemAtURL(url1, toURL: url2) 
+4
source

In addition, let _ = or _ = can be used when the right side of the expression is a lazy variable and you want it to be evaluated right now, but so far the value of this variable is not used.

A lazy stored property is a property whose initial value is not calculated before first use. You specify a lazy stored property by writing the lazy modifier before declaring it.

Lazy properties are useful when the initial value of a property depends on external factors whose values ​​are unknown until the initialization of the instances is completed. Lazy properties are also useful when the initial value of a property requires complex or computationally expensive tuning that should not be performed until it is needed.


Example:

 final class Example { private func deepThink() -> Int { // 7.5 million years of thinking return 42 } private(set) lazy var answerToTheUltimateQuestionOfLifeTheUniverseAndEverything: Int = deepThink() func prepareTheAnswer() { _ = answerToTheUltimateQuestionOfLifeTheUniverseAndEverything } func findTheQuestion() -> (() -> Int) { // 10 millions of thinking let theQuestion = { // something return self.answerToTheUltimateQuestionOfLifeTheUniverseAndEverything } return theQuestion } } let example = Example() // And you *want* to get the answer calculated first, but have no use for it until you get the question example.prepareTheAnswer() let question = example.findTheQuestion() question() 
+4
source

You can also use @discardableResult in your own functions, if sometimes you do not need a result, you want to use these functions without using a result.

 @discardableResult func someFunction() -> String { } someFunction() // Xcode will not complain in this case 
+1
source

All Articles