Hacker News

3 hours ago by s3cur3

Swift's Array type is just so well designed. It's a delight to use.

Two things stood out to me here as being really nice for someone coming from C++:

- Implicit upcasting to convert `[Derived]` to `[Base]` in O(1).

- Checked conversions convert `[T]` to `[U]?` in O(N) via `a as [U]` in O(n).

These are both operations I've manually written the C++ code to do, because there's no ergonomic way to do it with `std::vector`.

2 hours ago by jimbob45

Isnā€™t your second one just a simple ternary macro to check if null and return if so?

2 days ago by coldtea

>To achieve goals 1 and 2 together, we use static knowledge of the element type: when it is statically known that the element type is not a class, code and checks accounting for the possibility of wrapping an NSArray are eliminated. An Array of Swift value types always uses the most efficient possible representation, identical to that of ContiguousArray.

Why would elements being classes change this?

Wouldn't an array of class elements only hold pointers to those instances, so it wouldn't be different than "an array of Swift value types" at that regard (the extra dereference would only be needed after getting the element pointer out of the array).

4 hours ago by gilgoomesh

The point is that an NSArray can only hold classes (an Objective-C limitation), so if the element type is not a class, it can't be an NSArray.

an hour ago by bobbylarrybobby

I think that because a Swift array of objects must be bridgeable from Objective-C, and Objective-C arrays might be non-contiguous, Swift arrays of objects must be permitted to be non-contiguous. This isnā€™t a concern with arrays of structs because they donā€™t exist in Objective-C. In practice, Iā€™d imagine that Swift arrays of objects that are never bridges to/from Objective-C are almost always contiguous.

4 hours ago by schrodinger

Thought that was strange too, but if you read further it talks about having to use NSArray which isnā€™t always contiguous for class types to have free bridging with obj-c.

an hour ago by olliej

not sure why this is apparently being downvoted - this is a perfectly reasonable question for someone who isn't familiar with objective-c

Daily digest email

Get a daily email with the the top stories from Hacker News. No spam, unsubscribe at any time.