Classes, structs, enumerations • Classes passed by reference, structs - by value • Using a struct has no runtime penalty • All scalars and even Bool are structs • Enumerations are extremely powerful
Enumerations enum Tree { case … ! func depth() -> Int { func _depth(t: Tree) -> Int { switch t { case .Empty: return 0 case .Leaf(let _): return 1 case .Node(let lhs, let rhs): return max(_depth(lhs), _depth(rhs)) } } return _depth(self) } }
Enumerations enum Tree { case Empty case Leaf(T) case Node(Tree, Tree) ! func depth() -> Int { func _depth(t: Tree) -> Int { switch t { case .Empty: return 0 case .Leaf(let _): return 1 case .Node(let lhs, let rhs): return max(_depth(lhs), _depth(rhs)) } } return _depth(self) } }
What Swift is missing • Preprocessor • Exceptions • Access control * • KVO, KVC • Compiler attributes (platforms, deprecations, etc.) • performSelector: is unavailable
Objective-C bridging • Call Obj-c from Swift • Call Swift from Objc with limitations • Call CoreFoundation types directly • C++ is not allowed (should be wrapped in Objc) • Subclassing Swift classes not allowed in Objc
Swift internals • Swift objects are Obj-c objects • Implicit root class ‘SwiftObject’ • Ivars type encoding is stored separately • Method’s vtable • Name mangling
Pattern matching let point = (0, 1) ! switch point { case (0, 0): println("Point is at the origin") case (0...1, 0...1): println("I") case (-1...0, 0...1): println("II") case (-1...0, -1...0): println("III") case(0...1, -1...0): println("IV") default: println(“I don’t know.") }
Function currying func add(a: Int)(b: Int) -> Int { return a + b } ! let foo = add(5)(b: 3) // 8 ! let add5 = add(5) // (Int) -> Int let bar = add5(b: 3) // 8
Direct call of C functions @asmname - allows to provide a Swift interface for C functions @asmname("my_c_func") func my_c_func(UInt64, CMutablePointer) -> CInt;