I managed to catch a copy of beta 6 before it was pulled. Though not a copy of the release notes, so apologies if I duplicate some items (and hopefully don’t misspeak about stuff better explained in them!). On the assumption the binaries will be the same except re-signed, here’s a rundown of the changes to the standard library. (edit: they were)
Feels like Swift might be approaching the 1.0 home-stretch, with the focus moving to stability and Objective-C API interfacing. Nevertheless, plenty of changes to the Swift standard library in beta 6.
By far the largest swathe of changes are additional comments on existing types and functions, which are definitely worth a read and clarify several things. For example, a comment above Comparable
makes it clear you only need to define < to be comparable, despite Comparable
defining the comparators that aren't <.
Some small bits and pieces:
- The
??
operator has been updated to include a version to explicitly handle both the LHS and RHS being of the same optional type. I've updated my post with a comment, but it's still worth reading as a case study if you're writing a similar function. Array
now has aninit
that takes a_CocoaArrayType
, as well as anoCopy
flag, only to be set if the source array cannot be further mutated.AutoreleasingUnsafeMutablePointer
is no longer aBooleanType
, so no longer has aboolValue
propertyBit.Zero
andBit.One
are now capitalized (don't say we don't pay attention to detail here!)- The
Bool
constructor, which previously took a parameter ofBooleanType
(which worked becauseBooleanType
has no associated type requirements unlike, say,IntegerType
), now takes a generic parameterT
that must be ofBooleanType
. Interesting question to ponder is how this changes the function. COpaquePointer
has new constructors from raw memory addresses (these are described as “fundamentally unsafe”, you have been warned)Character
is nowComparable
- The
value
properties ofFloat
,Float80
andDouble
(which were ofBuiltin.FPIEEExx
) are gone. - The
FloatingPointType
protocol now includes constructors from all the built-in integer types. ImplicitlyUnwrappedOptional
no longer conforms toBooleanType
, though it still has itsboolValue
property (I should avoid using that if I were you).Optional
no longer has ahasValue
property. You should just use!= nil
RawOptionSetType
no longer implementsBooleanType
andEquatable
but instead implementsBitwiseOperationsType
- The
FIXME
s about howStrideThrough
andStrideTo
should be collections not sequences are gone. They're still sequences. UnicodeScalarView
is now reflectable.String
has a newextend
method that takes another string. This is in addition to the existingextend
that takes a sequence of characters.- It also has an
append
function that takes aUnicodeScalar
. String
is also nowComparable
.String
sunicodeScalars
property is now writeable.UnicodeScalar
now has aninit
forUInt16
andUInt8
in addition toUInt32
UnsafeMutableBufferPointer
has several changes. First, it is heavily commented. It's now aRandomAccessIndexType
(so you can calculate distances between them, and advance them). And its constructors have been changed to be consistent withCOpaquePointer
._ExtensibleCollectionType
has added anappend
function that appends a single element (all the implementors already support this)_RawOptionSetType
(and thusRawOptionSetType
) is nowEquatable
.contains
now has a version that takes an equatable element rather than a predicate.sorted
now takes any sequence, which is way less restrictive as before it required a mutable random-access collection.startsWith
now has a version that takes a comparison predicate (but likeequal
requires both sequences to contain the same type, even if the predicate could handle two different types).
transcode
, which I think officially has the longest function signature in the whole Swift library, got a teeny bit shorter as it now returns a (still a bit odd-looking) 1-tuple containing a Bool
, rather than a 1-tuple containing a Bool
labelled hadError
. Oh, by the way, you're not allowed to return 1-tuples with labels as of beta 6.
There is a new AssertString
type, which has a lower precedence for overloading purposes than StaticString
. How this is achieved is interesting, and a demonstration of how there are still inheritance hierarchies with structs: AssertString
implements a new AssertStringType
protocol. StaticString
also now implements a new protocol, StaticStringType
. StaticStringType
inherits from AssertStringType
. This means StaticString
is more specific than AssertString
and will therefore “win” in choices for which overload to pick (in the same way a function taking a CollectionType
wins over SequenceType
if an object supports it, or RandomAccessIndexType
wins over ForwardIndexType
). The protocols StaticString
previously implemented have moved to AssertStringType
There is a family of a new kind of assertion function, precondition
. The signatures are very similar to that of assert
. The comments suggest precondition
is a little stronger – they will still stop program execution even if assertions are turned off. Only with -Ounchecked
will they not check the condition. There's also a @noreturn preconditionFailure
function that doesn't check anything, just stops execution immediately.
Various new comments in the Swift library suggest use of these new preconditions. For example, a comment above GeneratorType.next
suggests calling preconditionFailure
if called a second time after nil
has already been returned.
There's a new protocol, RangeReplaceableCollectionType
, that defines several new operations on collections such as removing or replacing ranges, as well as insert
(insert an element into the middle of a collection), and splice
(insert a collection into the middle of the collection).
String
implements this new protocol. Interestingly, Array
(alongside ContiguousArray
and Slice
) does not appear to, though it does support all the methods (including new splice
and removeRange
functions) and if you write a generic function that takes a RangeReplaceableCollectionType
, you can pass an Array
into it. Declared somewhere more private I guess? Unless I'm missing a bit of indirection somewhere. If you spot it, let me know on twitter.
Following a familiar pattern, several of these new functions, such as slice
and the remove operations, are also availabe as non-member functions as well.
Finally, _BridgedToObjectiveCType
and _ConditionallyBridgedToObjectiveCType
appear to have coalesced into _ObjectiveCBridgeable
, but as ever I'll steer clear of discussing bridging topics.
[…] Changes to Swift in Beta 6. When I played around with Swift last week I was shocked how much of my code from June needed tweaking. I’m definitely going to wait until Swift settles down a little before going again. […]
Click to access xcode_6__beta_6_release_notes.pdf
xcode 6 beta 6 is still up with release notes, however ios 8 beta 6 isn’t available from the developer portal, still points to ios 8 beta 5
[…] String was extended in beta 6 to implement RangeReplaceableCollectionType, that means that, via inheritance, it also implements […]
[…] airspeedvelocity.net – Changes in the Swift Standard Library in Beta 6 […]