#86: @discardableResult in Swift

Swift warns when you ignore a function result because that often signals a mistake. If ignoring the result is perfectly reasonable, mark the function with @discardableResult.

@discardableResult
func log(_ message: String) -> String {
    print(message)
    return message
}

Now callers can use the return value when they want, but they are not forced to.

This is common for builder-style APIs, logging helpers and convenience wrappers.


I'm also on Twitter and GitHub.

Subscribe via RSS.