[swift-evolution] Make generics covariant and add generics to protocols

Chris Lattner clattner at apple.com
Wed Jan 13 19:51:26 CST 2016


> On Jan 13, 2016, at 3:02 PM, Austin Zheng via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi Howard,
> 
> Making generics covariant by default would add even more of a burden to users. They would need to check the type property of a generic object any time they wanted to mutate that generic object or risk their program terminating.
> 
> I don't think the fact that array accesses are checked at runtime is a good example:
> 
> 1. The Swift team has stated that subscripting into an array returns a non-optional at least in part because of performance issues. (Unfortunately, I think this was mentioned on the old Apple developer forums, which are now inaccessible, so don't take my word for it until someone with more insight says so one way or another :).

To clarify, the reason that array indexes don’t take and produce an optional is a bit more subtle than that.

1) subscripts have to be uniform for their getting and setter.  It would be weird & very confusing for "A[i] = nil” to work, given that you can’t remove an element from an array.

2) Integer array indexes are almost always derived from some other value that is in range.  Having to deal with the optional produced by a getter would almost always be noise.  That noise would force a lot of pointless optional manipulation, and would end up as a lot of !’s.  This would water down the optional model by making “!” a common thing, and be seen as less scary.

-Chris


More information about the swift-evolution mailing list