Swift 2: Beta 6
August 26, 2015
Some highlights from Beta 6:
Compiler & Tools
- A new
try?keyword has been added. This attempts an operation that may throw (fail). If it succeeds the result is wrapped in an optional. If it fails the error is ignored and
nilis returned. This seems like a pragmatic compromise but I have to imagine someone lost a battle somewhere because a lot of programmers are going to slap a
try?on it and ignore all errors.
- Using the
.EnumCasesyntax now provides code completions (yay)
- Static computed properties in protocol extensions are now supported
- Variadic parameters can appear anywhere in the parameter list; presumably the compiler disambiguates them by type and/or by consuming values off the right-hand side of argument list until the non-variadic arguments are satisfied. In any case say goodbye to yet another artificial legacy of C.
- Collections of non-Objective-C types are no longer considered
@objcthemselves so you'll get a compiler error if you try to force it with
@objc var prop:[SwiftOnlyType]
- Typedefs for block types (
typedef BOOL(^Doer)(void)) now import as Swift type aliases for closures. The release notes under-sell this by claiming it primarily allows
BOOLto import as
ObjCBool. The bigger benefit in my mind is human-readable type information on the Swift side. I prefer to create aliases for my block types to note the purpose of the block, e.g.
FlagChangedBlockeven though both may be
void(^)(BOOL)because it makes the code more self-documenting. Now that work carries over into Swift.
- Error messages produced by the type checker "continue to improve". If a type alias is involved the compiler will now helpfully include the alias and underlying type.
- Lots of improvements to
debugPrint(), including making them variadic and allowing you to specify your own terminator (instead of only supporting new line).
- Most APIs that take closures or
rethrowsproperly so you can pass throwing methods to
filter, etc. It also allows short-circuiting operators like
??to work with expressions that throw.
- A big change to collections, sequences, and splicing: All
CollectionTypes are now sliceable and
SequenceTypehas the notion of
SubSequenceto represent some of the values of the parent sequence in the same order. You can customize your own collections (like
ArraySlice) to provide windowed views onto sections of the parent sequence. Using copy-on-write for mutation and you can effectively slice and dice collections for free. There's a lot to unpack here so I suggest checking out the release notes for yourself.
- As part of this,
ArraySliceindices are no longer zero-based; they map directly into the indices of the collection they are slicing. Again, see the release notes for a good example of what was wrong with the old design and how the changes make things better.
- There is a nasty crash bug related to this; see below.
- As part of this,
guardat the top level of a file no longer causes memory corruption
- Taking the
inoutof an invalid expression no longer crashes the compiler (e.g.
Still Bugs or New Bugs
- Declaring multiple globals in a single
letmay cause corruption of their values; the workaround is to declare them with separate
- You still can't extend a Core Foundation type to conform to a protocol; casting to the protocol will fail in optimized builds.
- Array slices with a non-zero lower index are incorrectly displayed in the REPL and LLDB. Playgrounds with them will crash with
If I had to guess, I'd say the next release will be the GM release. There could be one more beta but I am not expecting any further changes. If the rumor mill (and past Septembers) are any guide, we'll see new hardware the second week of September and iOS 9 will GM along with Xcode 7 at the same time.
This blog represents my own personal opinion and is not endorsed by my employer.