[swift-dev] Swift's handling of function types

Jens Persson jens at bitcycle.com
Sat Aug 27 11:28:58 CDT 2016


IMHO Swift's handling of function types violate the principle of least
surprise.

In the example program below, are `a` and `b` really of the same function
type?

I searched but couldn't find any proposal or discussion addressing this.


// (Xcode 8 beta 6, toolchain: development snapshot 2016-08-26)

let a:  (Int, Int)  -> Int = { (a, b) in a + b }
let b: ((Int, Int)) -> Int = a
// So `a` can be casted to `b`'s type, OK.

print(type(of:a)) // ((Int, Int)) -> Int
print(type(of:b)) // ((Int, Int)) -> Int

// Why are they printed the same?
// I would only expect `b`'s type to look like that.
// Now it looks like both take a tuple of two Ints.

let c = a( 1, 2 ) // Can only be called this way, as expected.
let d = b((1, 2)) // Can only be called this way, as expected.
print(c) // 3, as expected.
print(d) // 3, as expected.

print(type(of:a) == type(of:b)) // true
// What? `a` clearly takes two Ints, while `b` clearly takes a
// tuple of two Ints, yet they are the same type?
// I am perplexed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160827/343bbbbb/attachment.html>


More information about the swift-dev mailing list