Xcode Beta 3 came out today, and with it a new version of the Swift standard library.
By far the majority of changes are to account for the publicized updates to the language:
- replacing
Type[]
with[Type]
, and making use of the new[Key:Val]
sugar for dictionaries - switching
uncheckedArithOp
forarithOpWithOverflow
in all the numeric types - adding explicit
bigEndian
andlittleEndian
initializers to the integers - replacing
succ
andpred
withsuccessor
andpredecessor
1 - adding
sorted
as an operation on arrays and as a standalone algo, sincesort
now sorts in-place2
copy
and unshare
have been removed from Array, since they’re redundant now arrays have full value semantics. Assigning an array to another variable copies it. Or rather, it lazily copies it – the array will only by physically copied when you alter one of the elements. So really, the equivalent of unshare
is called automatically when any value in the copied array or the copy is altered. You can still detect when this happens with the ===
operator, which returns true if two arrays are pointing to the same underlying store:
var a = [1, 2, 3, 4] let b = a // 'a' is "copied" // but really, it's still the same a === b // returns true a[2] = 2 // until you alter 'a' // and now they're different a === b // returns false
This is really important to understand, in case you start to panic about all the unnecessary copying that might happen when you pass an array into a function as a parameter.
There are also a handful of other changes/additions that aren’t mentioned in the release notes.
Range
now supports map
as a member, so you can write (1...5).map { $0 * 2 }
much like with an Array
, rather than map(1...5) { $0 * 2 }
. Note, Range.map
returns an Array
, unlike the map algorithm, which returns a lazily-computed view class.
Lots more types (such as Range
) now have a getMirror()
method.
min
and max
, which previously took two or more parameters (the “more” part being a variadic parameter) now have an overload with just two parameters. This means you can pass them as an argument to another function that expects a binary operator. So the following now compiles where previously it would throw an error:3
let a = [5, 3, 1, 7, 9] let b = [2, 4, 1, 1, 7] // now you can write this: let mins = map(Zip2(a, b), min) // instead of being forced to write this: let mins = map(Zip2(a, b)) { min($0, $1) }
Finally, the already mysterious insertionSort
got a bit more mysterious.
Previously there were two versions, both of which required a collection with a bidirectional index. One just required that the collection contents be comparable. The other allowed the caller to supply a predicate to compare elements with. That’s a pretty standard pattern, but oddly that predicate is declared inout
. Why a sort algorithm needs to be able to change the caller’s comparison function I do not know, but it can!4
The new insertionSort
that doesn’t take a predicate now requires a random-access index, rather than a bidirectional index. The version that takes the predicate still only requires a bidirectional index. It’s predicate is still inout
though.
There are a few other changes, relating to unicode character processing and Objective-C interfacing, that I either don’t feel qualified to comment on or haven’t delved into. Feel free to mention antyhing interesting about them in the comments.
- Bit odd, that, I preferred the shorter versions. ↩
- I really hate they did this to the non-member version, as it means you can’t chain it. ↩
-
If you don’t follow what this does –
Zip2
takes two sequences and “zips” them together, producing a new sequence that has pairs of elements from the same point in both sequences.map
then takes each element of this sequence and applies a function to each one. That function,min
, takes the pairs and returns the minimum. So the whole expression returns a sequence of the minimum of each position in either array. ↩ - It does mean you can’t call it with a trailing closure expression. You have to create a variable and point that at a function and then pass that variable in. That can’t be the reason, can it? ↩
[…] what we want for finding the minimum value in a sequence, with min as the combining function (as recently made possible in beta […]
[…] than access control,1 no big changes to the language with Beta 4, unlike with Beta 3. But still plenty of changes to the standard […]
[…] than access control, no big changes to the language with Beta 4, unlike with Beta 3. But still plenty of changes to the standard […]