Swift and/or compiler BUG? Labels in tuples part of the type?

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?

Replies

In this thread, I referred something like `compatible but different` types, you better have a glance.


And about this part:

but the compiler warning is very misleading.

You look to be too moderate to express this issue.

For me, it definately is a bug and should send a Bug Report soon.

Thanks for the reply.


Not quite sure what you mean about me being moderate, but I've reported it as a possible bug and at least something that needs clarification.

Not quite sure what you mean about me being moderate

Sorry, some parts of my posts are helped by web translating service and with the italic moderate, I wanted to represent my feeling that I'm not sure if it's appropriate for this situation.


Anyway, I also expect this sort of things should be clarified and really appreciate you took some time to write a bug report. May Swift team utilize it to improve the documentations.

No problem - just wasn't sure.


I've filed the bug - let's hope something happens.


Have a good day.


🙂