[swift-evolution] Casting Bug Swift

Jordan Rose jordan_rose at apple.com
Thu Feb 25 11:07:39 CST 2016


I think there are two things going on here:

- The compiler allows a memory-corrupting cast, which should be rejected (a bug).
- But it's a useful thing to do (a language change).

I'm actually surprised about the bug, since we get plenty of questions about why the compiler doesn't allow it, implying that the compiler is indeed rejecting it. The answer is that protocol values and class values don't have the same representation, so converting between [Eatable] and [Burger] is an O(N) operation that requires allocating a new array. But that may not be a good enough reason not to allow it—conversions from NSArray to Array can do the same thing if the NSArray was mutable.

Jordan


> On Feb 25, 2016, at 8:53 , Radosław Pietruszewski via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hey James,
> 
> The appropriate place for compiler bugs is https://bugs.swift.org <https://bugs.swift.org/>, not Swift Evolution.
> 
> Best,
> — Radek
> 
>> On 25 Feb 2016, at 17:44, James Campbell via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Say I have been passed an array of objects that conform to a protocol called "Eatable" i.e [Eatable]
>> 
>> If I then try to cast them to an array of classes who happen to implement that protocol i.e [Burger]
>> 
>> The compiler lets me but in the runtime it corrupts the memory.
>> ___________________________________
>> 
>> James⎥iOS Lead
>> 
>> james at supmenow.com <mailto:james at supmenow.com>⎥supmenow.com <http://supmenow.com/>
>> Sup
>> 
>> Runway East
>> 
>> 
>> 10 Finsbury Square
>> 
>> London
>> 
>> 
>> EC2A 1AF 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160225/04206cc1/attachment.html>


More information about the swift-evolution mailing list