Using @discardableResult in Swift
Learn how to safely ignore the result of a function or method in Swift using the @discardableResult attribute.
We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Although the function returns a value, the compiler shouldn’t generate a warning if the return value is unused. This is what is described about the attribute @discardableResult
in the swift programming language guide. With this definition I did not fully realize when one should use this.
@discardableResult
in Swift?
In Swift, @discardableResult
is an attribute that you can apply to a function or method declaration to indicate that the result of calling that function or method can be safely ignored.
This means that the compiler won’t generate a warning if the result of the function or method is not used. This is particularly useful for functions that have a return value that is not necessarily needed by the caller.
By using @discardableResult
, you can give the caller the flexibility to choose whether they want to use the result of a function or method, without generating any warnings if they choose to ignore it.
@discardableResult
To use @discardableResult
, you simply apply it as an attribute to the function or method declaration. Here’s an example of how to use it with a function that adds two integers and returns the result:
class Calculator {
@discardableResult
func add(_ x: Int, _ y: Int) -> Int {
return x + y
}
}
let calc = Calculator()
calc.add(2, 3) // returns 5, but the result is ignored
The difference between using @discardableResult
and returning Void
is that @discardableResult
indicates that the function returns a result that can be safely ignored, whereas returning Void means that the function doesn’t return anything at all.
When a function returns Void,
the caller can still choose to ignore the result, but the compiler won’t generate a warning in that case, because there’s no result to ignore.
On the other hand, when a function is marked with @discardableResult
, the compiler will generate a warning if the result is not used, unless the function is explicitly marked as @discardableResult
.
@discardableResult
There are several reasons:
By marking a function with@discardableResult
, you give the caller the flexibility to choose whether they want to use the result or not, without generating any warnings if they choose to ignore it.
This can be useful in cases where the caller doesn’t necessarily care about the result of the function (e.g. if it’s a background task that can be retried later), or if the caller handles error conditions in a separate way (e.g. by presenting a user-friendly error message).
If a function has a return value that is not necessarily needed by the caller, but the compiler generates a warning if the result is ignored, it can be easy to accidentally assign the result to a variable when you don’t actually need it.
By using @discardableResult
, you can avoid these accidental mistakes and make your code more readable and maintainable.
By marking functions with @discardableResult
, you can make your APIs more flexible and easier to use. By giving the caller the option to ignore the result of a function, you can make the API more intuitive and easier to understand.