[swift-evolution] Standard library 'Data' type pre-proposal

Dmitri Gribenko gribozavr at gmail.com
Wed May 11 13:01:18 CDT 2016


On Wed, May 11, 2016 at 2:37 AM, Austin Zheng via swift-evolution
<swift-evolution at swift.org> wrote:
> Hello swift-evolution,
>
> I've been thinking about a standard library 'Data' type for a while,
> analogous to NSData in the same way Swift's Arrays and Dictionaries are
> analogous to NSArrays and NSDictionaries. A first-class container for binary
> data that is available to every Swift user, conforms to Swift semantics, and
> is safer and easier to work with than UnsafeBufferPointer seems like a
> natural fit for the standard library.

Hi Austin,

This is an interesting territory!

One thing that I would like to suggest for us to consider is
justifying why Data needs to be a separate type from Array<Int8> and
Array<UInt8>.  We can add conditional extensions to Array of Int8 and
UInt8 if we find that existing NSData/dispatch_data_t usecases need a
few special APIs that won't make sense on arrays in general.

For example, something that I would imagine people want to do with
"data buffer" types is being able to make an unaligned or type punned
load or store.  For example, in Java, this is one of the primary
usecases for a type similar in spirit, java.nio.ByteBuffer
(https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html).

Another usecase that is a crossover between Array and Data, allow
Array to (unsafely) adopt ownership of an existing initialized unsafe
buffer pointer.  We had quite a few requests for this.  Do you think
this is an interesting usecase?  Does it overlap with this discussion?

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list