Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @discardableResult for Closures in Swift

Swift 3 has introduced the @discardableResult annotation for functions to disable the warnings for an unused function return value.

I'm looking for a way to silence this warning for closures.

Currently, my code looks like this:

func f(x: Int) -> Int -> Int {
  func g(_ y: Int) -> Int {
    doSomething(with: x, and: y)
    return x*y
  }
  return g
}

In various places I call f once to obtain a closure g which I then call repeatedly:

let g = f(5)
g(3)
g(7)
g(11)

In most places I'm only interested in the side effects of the nested call to doSomething, and not in the return value of the closure g. With Swift 3, there are now dozens of warnings in my project for the unused result. Is there a way to suppress the warnings besides changing the calls to g to _ = g(...) everywhere? I couldn't find a place where I could place the @discardableResult annotation.

like image 633
Theo Avatar asked Sep 26 '16 21:09

Theo


People also ask

What is @discardableResult in Swift?

While writing methods in Swift you're often running into scenarios in which you sometimes want to ignore the return value while in other cases you want to know the return value. The @discardableResult attribute allows us to enable both cases without having to deal with annoying warnings or underscore replacements.

How do you use closures in Swift?

Closure Parameters Inside the closure, (name: String) specifies that the closure accepts the String type parameter named. Notice that we have used in to separate closure parameter with body. Here, we have passed a string value "Delilah" to our closure. And finally, the statement inside the closure is executed.

How do you declare a closure variable in Swift?

Syntax Of Closure: We're declaring the closure on the first line, then assign it to the constant birthday , and call the closure on the last line. The closure now has one parameter of type String . It's declared within the closure type (String) -> () . You can then use the parameter name within the closure.

What is the advantage of closure in Swift?

Closures are self-contained blocks of functionality that can be passed around and used in your code. You should use closure(s) when you want to pass a chunk of code as a parameter to a method that you want to execute it asynchronously.

What is the use of discardable result in Swift?

While writing methods in Swift you’re often running into scenarios in which you sometimes want to ignore the return value while in other cases you want to know the return value. The @discardableResult attribute allows us to enable both cases without having to deal with annoying warnings or underscore replacements.

What is a closure in Swift?

In Swift, a closure is a special type of function without the function name. For example, Here, we have created a closure that prints Hello World. Before you learn about closures, make sure to know about Swift Functions. We don't use the func keyword to create closure. Here's the syntax to declare a closure: var greet = { print("Hello, World!") }

How do you use @available in Swift?

@available ( swift 3.0. 2 ) Apply this attribute to a function or method declaration to suppress the compiler warning when the function or method that returns a value is called without using its result. Apply this attribute to a class, structure, enumeration, or protocol to treat instances of the type as callable functions.

What is discardableresult in C++?

For example, the discardableResult attribute on a function declaration indicates that, although the function returns a value, the compiler shouldn’t generate a warning if the return value is unused. You specify an attribute by writing the @ symbol followed by the attribute’s name and any arguments that the attribute accepts:


1 Answers

I don't think there's a way to apply that attribute to a closure. You could capture your closure in another that discards the result:

func discardingResult<T, U>(_ f: @escaping (T) -> U) -> (T) -> Void {
  return { x in _ = f(x) }
}

let g = f(5)
g(3)  // warns
let h = discardingResult(g)
h(4)  // doesn't warn
like image 163
Nate Cook Avatar answered Nov 14 '22 02:11

Nate Cook