When assigning a tuple of, say, Int members, to a tuple of an (heterogeneous) protocol type, to which Int conforms, it is seemingly only allowed to perform this assignment via explicit member-by-member assignment.
protocol MyType {}
extension Int: MyType {}
let intPair = (1, 2)
var myTypePair : (MyType, MyType)
// OK
myTypePair = (intPair.0, intPair.1)
// OK
let intPairToMyTypePair : ((Int, Int)) -> (MyType, MyType) = { ($0.0, $0.1) }
myTypePair = intPairToMyTypePair(intPair)
// For all below:
// "Error: cannot express tuple conversion '(Int, Int)' to '(MyType, MyType)'"
myTypePair = intPair
myTypePair = (intPair as! (MyType, MyType))
/* warning: forced cast from '(Int, Int)' to '(MyType, MyType)'
always succeeds <-- well, not really */
if let _ = intPair as? (MyType, MyType) { }
/* warning: conditional cast from '(Int, Int)' to '(MyType, MyType)'
always succeeds */
The peculiarity is that for the casting cases above the Swift compiler warns that
warning: forced/conditional cast from
'(Int, Int)'to'(MyType, MyType)'always succeeds
Which it clearly does not:
error: cannot express tuple conversion
'(Int, Int)'to'(MyType, MyType)'
Question: What is the issue here, is the non-allowance of direct assignment as intended? If so, what's up with the warning message that the casting will always succeed?
(Edit addition)
To clarify further the peculiar behaviour I'm asking about here: I wonder why the follow snippet prints "bar" even if Swift warns us that the 'is' case is always true:
let intPair = (1, 2)
switch intPair {
case is (MyType, MyType): print("foo") /* warning: 'is' test is always true */
case _ : print("bar")
}
// "bar"
The analogous case for an array is straightforward with explanatory errors, but I don't know if this is a valid comparison, as tuples are more of anonymous structures rather than some cousin to Array.
let intArr = [Int](1...5)
var myTypeArr : [MyType]
myTypeArr = intArr
/* error: cannot assign value of type '[Int]' to type '[MyType]' */
if let _ = intArr as? [MyType] { }
/* error: 'MyType' is not a subtype of 'Int' */