Swift: Beta 5
The more things change
In the fast-moving world of Swift, I'm a day late and that might as well be a year. But I'm also a developer so I just can't help myself. Let's dive into the changes in Beta 5!
Optionals
- As expected, the null coalescing operator was added
??
and lazily evaluates the right-hand side again as you'd expect. - Optionals no longer implement
LogicValue
, so you must explicitly compare with nil. - Unwrapped optionals "just work", so
opt!++
behaves as if it were a non-optional whenopt
has a value - Apple has begun the process of auditing the APIs to remove the implicitly unwrapped Optional where the API is known to never return nil. Eventually this should make the API contracts much more explicit and self-documenting as to whether they can return nil or not.
Interop
- The unsafe pointer types have been renamed to
UnsafePointer
andUnsafeMutablePointer
. EssentiallyUnsafePointer
swapped meaning and now represents a const pointer. - The unsafe array types were renamed to
UnsafeBufferPointer
andUnsafeMutableBufferPointer
On a related note:
@objc
now only makes things visible to Objective-C. Previously it also made things essentially virtual, or dynamic dispatch as the docs refer to it.- A new first-class language concept has been introduced with the
dynamic
keyword. This marks a method, property, subscript, or initializer as always dynamically dispatched. It is never inlined or devirtualized. dynamic
is currently restricted to@objc
types because it uses the Objective-C runtime underneath. That's also whydynamic
enables KVO and other such Cocoa magic. The docs seem to hint that this will not always be the case in the future (presumably Swift will gain native dynamic capabilities).
Update: I was a bit off on dynamic
. Overridden members of superclasses are still virtual in Swift, but if the compiler can prove it safe, it will de-virtualize or inline the call. dynamic
prevents that optimization and forces dispatch to use standard Objective-C runtime facilities. final
still marks something as not overridable by a subclass.
Operators
- The attribute to keyword conversion continues;
prefix
,postfix
, andinfix
are now keywords in both the operator declaration and the operator implementation functions - The order of operator declarations was reversed, so it's now
prefix operator
. - Assignment operators no longer use the
@assignment
attribute
Attributes
@class_protocol
is gone; now you declareprotocol P : class
to accomplish the same thing@auto_closure
had the underscore excised; it's now@autoclosure
Documentation comments
- They have made
///
explicit as a documentation comment marker - They also explicitly stated that reStructuredText is the markup format.
Classes
- Overriding a designated initializer must now be explicit with the
override
keyword - If an initializer is required, the subclass indicates this with
required
.
Playgrounds
println()
now works as you'd expect in playgrounds (being associated with the line where called, not just printed in the console output)- You can now import frameworks!
- Both the Mac and iOS playgrounds are now fully sandboxed, so your amusing file manipulation code will no longer erase your entire disk.
Other Changes
- If you cast an array from Objective-C, Swift now defers the type-checking until each individual element is accessed. This should improve performance when tossing arrays back and forth.
- They removed
+=
for appending to an array. Not personally a fan of this one but I understand why. - A bunch of protocols were renamed with the goal that they will all end with "ible", "able", or "Type". So
Integer
becomesIntegerType
. This is a bit more finicky than the C# equivalent of interfaces starting with "I", but seems workable enough. - Ranges - Huge changes to this; I suggest reading up the documentation.
On a personal note, I'm moving to the SF Bay area soon and my wife is having a baby so posting may be a bit light over the next few weeks. Having a baby and moving right after doesn't represent great timing, but you can't always wait for the perfect moment.
This blog represents my own personal opinion and is not endorsed by my employer.