[swift-evolution] [Pitch] Adding safety to arrays

Dave Abrahams dabrahams at apple.com
Mon Apr 17 11:35:45 CDT 2017

on Fri Apr 14 2017, Karl Wagner <swift-evolution at swift.org> wrote:

> Personally, the only valid use-case I can think of is when you want to
> initialise an Array’s elements out-of-order - i.e., you want to set a
> value for myArray[2] despite myArray[0] and [1] not being
> populated. In that case, it would be better to have some kind of
> SparseArray type, and for us to have a proper API for unsafe
> initialisation of stdlib types.

That is a point in the design space, but there's really no need to
expose unsafe initialization in order to get that functionality.  You
could simply build ArrayOfOptional<T> that would present the same API as
Array<T?> but would use a separate inline buffer of bits to denote which
of the optionals were non-nil.


More information about the swift-evolution mailing list