Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the shorthand argument name $0 returning a tuple of all parameters?

Tags:

swift

I was trying to define a variable that can hold a closure and ran into some difficulties using Swift's shorthand argument names. Take the following code snippet:

var returnAString: (String, String) -> String
returnAString = { return $0 }

This gives me the compile error '(String, String)' is not convertible to 'String'. When I modify the closure return a string with the first argument value, I get back both argument values in a tuple.

println(returnAString("1", "2")) // Prints "(1, 2)"

However, if I modify the closure to print the second argument the correct argument is printed as expected.

returnAString = { $1 }
println(returnAString("1", "2")) // Prints "2"

I've got the feeling that I'm doing something a little silly here, but I couldn't find any explanation of why this would be happening.

like image 305
jaker Avatar asked Sep 01 '14 13:09

jaker


People also ask

What is autoclosure in Swift?

@autoclosure in Swift is a type of closure that allows to omit braces and make it look like a normal expression. Under the hood, however, it's still a closure. By understanding what this means, we can improve the efficiency of our code. The @autoclosure keyword might be new to you.

What is closure expression?

Closure expressions are a way to write inline closures in a brief, focused syntax. Closure expressions provide several syntax optimizations for writing closures in a shortened form without loss of clarity or intent.

What is trailing closure?

A trailing closure is written after the function call's parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don't write the argument label for the closure as part of the function call.

How to declare a closure in Swift?

Swift Closure Declarationparameters - any value passed to closure. returnType - specifies the type of value returned by the closure. in (optional) - used to separate parameters/returnType from closure body.


1 Answers

Closures § Shorthand Argument Names

Shorthand Argument Names

… the number and type of the shorthand argument names will be inferred from the expected function type.

It looks like this is the expected behavior. By providing only $0 you are inferring (at least to the system) that you want a tuple.

This only seems to be a special case of using $0. For example, the following cannot be compiled.

var returnAString3: (String, String, String) -> String
returnAString3 = { return $1 }

No matter how it's modified.

returnAString3 = { return $1.0 } // still fails.
like image 80
Jeffery Thomas Avatar answered May 24 '23 12:05

Jeffery Thomas