Swift 4: Discardable Result

Swift 2.2

In Swift 2.2, if you called a function and didn’t use its returned result you would receive no warning. To generate a warning the method definition itself would need to be annotated with:

@warn_unused_result

i.e.

which produces the following warning when used:

Swift 3 and Swift 4

This behaviour has been flipped around in Swift 3 and Swift 4 where now any unused returned results generate a warning. If you’re designing an API and certain function return values should more so be treated as side effects you can annotate the function with:

@discardableResult

i.e.

The swift proposal for this change can be read here

Like what you read? Give Patrick a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.