Monday, June 23, 2014

Advanced Swift - Part 2

This is a summary of the topics and notes that I found interesting from watching the Advanced Swift WWDC session 404 by John McCall and Dave Abrahams. I highly recommend you watch the session. It also has some commentary from me which you may or may not agree with. Please feel free to join the conversation. You can find Part 1 here.

I have also attached a swift playground file with the sample code for part 2.

Swift Language Protocols

In swift, you can hook into language features by using special protocols. Swift is very much a protocol based language. Some of the language protocols include:

LogicValue if LogicValue {
Printable "\(printable)
Sequence for x in sequence
IntegerLiteralConvertible 65536
FloatLiteralConvertible 1.0
StringLiteralConvertible "abc"
ArrayLiteralConvertible [ a, b, c ]
DictionaryLiteralConvertible [ a:x, b:y ]
Equatable x == y

Let's look at a simple example of implementing the Printable protocol that allows you to provide a custom description that will be used by println.

Unfortunately when using the playground, the Printable protocol isn't used. I assume this will be fixed at some point.

Subscript Declarations

You can use Swift's subscript declarations to add subscripts and even support for subscript ranges. Here is a useful example adapted from a Stack Overflow post that extends the String class to add subscript access to Strings.

This allows for natural String manipulations, which I think should actually be built into Swift:

var string     = "abcdef"
var firstChar  = string[0]
var subString  = string[0...2] // subString equals "abc"
var subString2 = string[2..4]  // subString2 equals "cd"

While you can have your subscript code perform arbitrary work, care should be used to make them behave in a way that is consistent with what people would expect subscripts to do.

Operator Overloading

Swift also supports operating overloading. I'm not a fan of operating overloading as I think it creates maintainability issues. I would advice to use operating overloading very cautiously.

Operators are defined using global functions so they are available outside the scope of the classes they work with:

We also added a new method to our Thing class which is nameWithArticle so it can provide a basic implementation of using "a" vs "an".

The next feature is one that I was hoping Apple was going to bring to Objective-C and that is Generics.


Generics is a powerful concept that allows you to reuse code while preserving and leveraging strong typing. In fact, we have already used generics (see the Range parameter under Subscript Declarations). No more using an id, NSObject, or in Swift the Any type unless you need something to be truly typeless such as a collection that can hold multiple types or for dynamic polymorphism.

Here is a simple example that shows a generic function that just prints the given value to the console and then returns that value back out. 

Most compilers treat generics as templates and generate additional code for each type instance of the generic. However, Swift doesn't to do this. It only has one instance of the code for its generics. In addition, the Swift compiler generates more efficient code when it can make optimizations based on the type. 

You can also constrain the type information to force it to conform to a protocol. This allows the compiler to know what operations can be performed by the type.  For example:

The above example uses the Equatable protocol to let the compiler know it can perform the "==" operator. There is a lot more on equatable and generics in the session, and I encourage you to watch it. There is some very cool stuff that Swift can do with its generic system. There is a mind blowing example of a generic "memorize" function that is worth studying.

Wrap Up

Lots of people are starting to learn Swift, including myself, so I hope this was helpful. If you see anything incorrect or if you have different opinions please share your thoughts.

You can download the playground file which will allow you to play with and copy and paste all the code used. Note that you will need XCode 6 to run the example.

Tod Cunningham
Five Lakes Studio, LLC

No comments:

Post a Comment