What is discardableResult in Swift?
language
When a function is marked with @discardableResult
, it allows the caller to ignore the return value of the function.
For example let’s consider a function that returns if an operation was successful.
Now when the caller calls this function the compiler will suggest that Result of call to setPersonName
is unused.
To silence the compiler you have to create a variable to store the Bool value returned from the function.
But in certain situations the caller might not care about the result of the call. Marking the function with discardableResult silences the compiler warning and allows you to ignore the return value from the function.
Compiler will not warn you anymore when you invoke the function setPersonName(name: "Joe")
.
Sponsored