I have asked a similar question before, but this is a much shorter and clearer version of it.
Carefully study this simple program:
(EDIT: On line 3, note that <T> and <T: Any> is the same thing, since "typealias Any = protocol <>" is a protocol composition of no protocols, which means it is constraining T with the constraint of no constraint.)
func foo<T: IntegerType>(v: T) { print(" Called foo<T: IntegerType>(\(v))") }
func foo<T: FloatingPointType>(v: T) { print(" Called foo<T: FloatingPointType>(\(v))") }
func foo<T: Any>(v: T) { print(" Called foo<T: Any>(\(v))") }
func bar<T, R>(v: T, fn: T -> R) -> R {
return fn(v)
}
func baz<T>(v: T) -> Void {
return foo(v)
}
print("foo-test:")
foo(111)
foo(1.1)
print("bar-test:")
bar(222, fn: foo)
bar(2.2, fn: foo)
print("baz-test:")
baz(333)
baz(3.3)
Here's the results (Xcode 7 beta 2):
foo-test:
Called foo<T: IntegerType>(111)
Called foo<T: FloatingPointType>(1.1)
bar-test:
Called foo<T: IntegerType>(222)
Called foo<T: FloatingPointType>(2.2)
baz-test:
Called foo<T: Any>(333)
Called foo<T: Any>(3.3)
To me, foo-test and bar-test behave as expected, but baz-test surprises me.
Is the output of baz-test different than the other two because of a compiler bug?
If no, please explain (in as much detail you can) why baz-test and bar-test produce different outputs.