Autoclose that accepts arguments but returns no value

The auto-close function according to the Swift docs states that

An autoclose is a closure that is automatically generated for an expression passed as an argument to a function. This takes no arguments, and when called, it returns the value of the expression that is wrapped inside it.

But when I create a function that takes autoclose

func test(_ clsr:@autoclosure(String)->(String)){
   let res = clsr("Hello World")
   print(res)
}
test("Good Morning")

      

Even the syntax is valid and I can pass the value. I cannot use a String value inside a statement. So this is something quick. Perhaps an error warning should appear when defining the arguments.

Or am I missing something about autoclose?

+3


source to share


1 answer


Autocomplete does not accept ANY arguments

You can define autoclose with a type parameter @autoclosure(String)->(String)

, but every parameter in that closure will be ignored

Example 1



func test(_ clsr: @autoclosure(String)->(String)){
   print(clsr("IgnoredText")) // "Good Morning"
}
test("Good Morning")

      

Example 2

func test(_ clsr: @autoclosure()->(String)){
   print(clsr()) // "Good Morning"
}
test("Good Morning")

      

+1


source







All Articles