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:
??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.
Arraynow has an
initthat takes a
_CocoaArrayType, as well as a
noCopyflag, only to be set if the source array cannot be further mutated.
AutoreleasingUnsafeMutablePointeris no longer a
BooleanType, so no longer has a
Bit.Oneare now capitalized (don't say we don't pay attention to detail here!)
Boolconstructor, which previously took a parameter of
BooleanType(which worked because
BooleanTypehas no associated type requirements unlike, say,
IntegerType), now takes a generic parameter
Tthat must be of
BooleanType. Interesting question to ponder is how this changes the function.
COpaquePointerhas new constructors from raw memory addresses (these are described as “fundamentally unsafe”, you have been warned)
Double(which were of
Builtin.FPIEEExx) are gone.
FloatingPointTypeprotocol now includes constructors from all the built-in integer types.
ImplicitlyUnwrappedOptionalno longer conforms to
BooleanType, though it still has its
boolValueproperty (I should avoid using that if I were you).
Optionalno longer has a
hasValueproperty. You should just use
RawOptionSetTypeno longer implements
Equatablebut instead implements
FIXMEs about how
StrideToshould be collections not sequences are gone. They're still sequences.
UnicodeScalarViewis now reflectable.
Stringhas a new
extendmethod that takes another string. This is in addition to the existing
extendthat takes a sequence of characters.
- It also has an
appendfunction that takes a
Stringis also now
unicodeScalarsproperty is now writeable.
UnicodeScalarnow has an
UInt8in addition to
UnsafeMutableBufferPointerhas several changes. First, it is heavily commented. It's now a
RandomAccessIndexType(so you can calculate distances between them, and advance them). And its constructors have been changed to be consistent with
_ExtensibleCollectionTypehas added an
appendfunction that appends a single element (all the implementors already support this)
RawOptionSetType) is now
containsnow has a version that takes an equatable element rather than a predicate.
sortednow takes any sequence, which is way less restrictive as before it required a mutable random-access collection.
startsWithnow has a version that takes a comparison predicate (but like
equalrequires 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
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
StaticString also now implements a new protocol,
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
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,
Slice) does not appear to, though it does support all the methods (including new
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.
_ConditionallyBridgedToObjectiveCType appear to have coalesced into
_ObjectiveCBridgeable, but as ever I'll steer clear of discussing bridging topics.