Consider the following:
struct SomeStruct {}
var foo: Any!
let bar: SomeStruct = SomeStruct()
foo = bar // Compiles as expected
var fooArray: [Any] = []
let barArray: [SomeStruct] = []
fooArray = barArray // Does not compile; Cannot assign value of type '[SomeStruct]' to type '[Any]'
I've been trying to find the logic behind this, but with no luck. It's worth mentioning if you change the struct to a class, it works perfectly.
One could always add a workaround and map each object of the fooArray and cast them to type of Any, but that is not the issue here. I'm looking for an explanation on why this is behaving like it is.
Can someone please explain this?
Swift 3 Update
As of Swift 3 (specifically the build that ships with Xcode 8 beta 6), collection types can now perform under the hood conversions from value-typed element collections to abstract-typed element collections.
This means that the following will now compile:
Pre Swift 3
This all starts with the fact that generics in Swift are invariant – not covariant. Remembering that
[Type]
is just syntactic sugar forArray<Type>
, you can abstract away the arrays andAny
to hopefully see the problem better.Similarly with classes:
This kind of covariant behaviour (upcasting) simply isn't possible with generics in Swift. In your example,
Array<SomeStruct>
is seen as a completely unrelated type toArray<Any>
due to the invariance.However, arrays have an exception to this rule – they can silently deal with conversions from subclass types to superclass types under the hood. However, they don't do the same when converting an array with value-typed elements to an array with abstract-typed elements (such as
[Any]
).To deal with this, you have to perform your own element-by-element conversion (as individual elements are covariant). A common way of achieving this is through using
map(_:)
:A good reason to prevent an implicit 'under the hood' conversion here is due to the way in which Swift stores abstract types in memory. An 'Existential Container' is used in order to store values of an arbitrary size in a fixed block of memory – meaning that expensive heap allocation can occur for values that cannot fit within this container (allowing just a reference to the memory to be stored in this container instead).
Therefore because of this significant change in how the array is now stored in memory, it's quite reasonable to disallow an implicit conversion. This makes it explicit to the programmer that they're having to cast each element of the array – causing this (potentially expensive) change in memory structure.
For more technical details about how Swift works with abstract types, see this fantastic WWDC talk on the subject. For further reading about type variance in Swift, see this great blog post on the subject.
Finally, make sure to see @dfri's comments below about the other situation where arrays can implicitly convert element types – namely when the elements are bridgeable to Objective-C, they can be done so implicitly by the array.
Swift can't automatically convert between array which holds value types and reference types. Just map the array to the type you need:
fooArray = barArray.map({ $0 }) // Does compile