Swift 3 question on a function type

I have read some threads about this but here is my question. I use beta 3.


func foo1(x: Int) -> Int { ... }


func foo2(x: Int, y: Double) -> Double { ... }


func foo3() -> Int { ... }


If I ask for the dynamicType of these functions, I get :

foo1: (Int) -> Int

foo2: ((Int,Double)) -> Double

foo3: (()) -> Int


I don't understand why those extra parentheses in the lhs of the types !?

The type of foo1 seems to say that the only parameter is an Int, and in the same vein

the type of foo2 should say by coherence that foo2 waits for a tuple (Int,Double).

Moreover I have read that (Int) == Int. So what ?

Thanks.

Same in beta 6...

I don’t have a definitive answer for you here but I believe this is fallout from Swift’s gradual move from ‘function arguments are just tuples’ to ‘function arguments and, well, function arguments’.

From SE-0029:

From a historical perspective, the tuple splat form of function application dates back to very early Swift design (probably introduced in 2010, but possibly 2011) where all function application was of a single value to a function type. For a large number of reasons (including inout, default arguments, variadic arguments, labels, etc) we completely abandoned this model, but never came back to reevaluating the tuple splat behavior.

SE-0110 fixes another edge case related to this move:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks eskimo, but please elaborate on the splat form of function please.

These two functions below have the same type ((Int, Int)) -> Int

Something seems to be wrong here, or I don't get the right

point of vue. Where is this behavior cited in the doc ?


func foo(x: Int,y: Int) -> Int {

return x + y

}


func bar (t : (Int,Int)) -> Int {

return t.0 + t.1

}


Moreover, may we compute on types ? I mean, may I get the lhs of

the type ((Int,Int)) -> Int ?

Don't you miss a meta-paren, say a bracket, to distinguish :

[Int,Int] -> Int binary function waiting for two Int

(Int,Int) -> Int unary function waiting for a tuple of Int

My 0.2 cents.

Swift 3 question on a function type
 
 
Q