I'm strugging with what seems like an inconsistency either in Swift or the Swift Compiler.
Consider the case of two tuple types that are identical except one has labelled elements and the other does not. Are the labels part of the type, and if so, surely a test with 'is' can be used to indicate this.
Paste the following into a playground. Note the compiler warning given on line 15: " 'is' test is always true " whereas the output says "DIFFERENT TYPE". This seems contradictory?
For line 21, the output is "SAME TYPE" as expected.
import UIKit
//: This tuple does not use labels
let theDate : (String, Int) = ("January", 30)
//: This works without an explicit type conversion - suggests they are the same type?
let theDateLabelled : (month:String, day:Int) = theDate
theDateLabelled.month
// theDate.month //As expected, this line (if uncommented) does not compile
//: The warning below says `is` test is always true, yet the test fails. **This is surely contradictory?**
if (theDate is (month:String, day:Int)) {
print("SAME TYPE")
} else {
print("DIFFERENT TYPE")
}
if (theDate is (String, Int)) {
print("SAME TYPE")
} else {
print("DIFFERENT TYPE")
}
//: It is not clear whether tuples with different labels that encapsulate the same type of data are strictly the same type or not?
This suggests they are not the same type (which makes some sense), but the compiler warning is very misleading. Furthermore, it seems slightly inconsistent that a type-cast is not required, but given this is still safe, I guess it's less of an issue?