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.
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.
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.
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.
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.
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.
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!") }
@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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With