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 andnil
is 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 atry?
on it and ignore all errors. - Using the
.EnumCase
syntax 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
@objc
themselves 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 allowsBOOL
to import asBool
instead ofObjCBool
. 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.CancellationHandlerBlock
orFlagChangedBlock
even though both may bevoid(^)(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.
Standard Library
- Lots of improvements to
print()
anddebugPrint()
, including making them variadic and allowing you to specify your own terminator (instead of only supporting new line). extend
was renamedappendContentsOf
andsplice
was renamedinsertContentsOf
- Most APIs that take closures or
@autoclosure
parameters userethrows
properly so you can pass throwing methods tomap
,filter
, etc. It also allows short-circuiting operators like&&
,||
, or??
to work with expressions that throw. - A big change to collections, sequences, and splicing: All
CollectionType
s are now sliceable andSequenceType
has the notion ofSubSequence
to represent some of the values of the parent sequence in the same order. You can customize your own collections (likeArray
andArraySlice
) 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,
ArraySlice
indices 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,
Bug Fixes
- Using
guard
at the top level of a file no longer causes memory corruption - Taking the
inout
of an invalid expression no longer crashes the compiler (e.g.&some_invalid_thing
)
Still Bugs or New Bugs
- Declaring multiple globals in a single
var
orlet
may cause corruption of their values; the workaround is to declare them with separatevar
orlet
statements. - 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
EXC_BAD_INSTRUCTION
.
Upcoming release
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.
Russ Bishop
This blog represents my own personal opinion and is not endorsed by my employer.