Denis Lebedev, Swift

32,861 views

Published on

Published in: Technology, Business

Denis Lebedev, Swift

  1. 1. Swift Denis Lebedev, iOS @ Yandex
  2. 2. Agenda • Introduction • Objective-C bridging • Good-to-know features • “Where can I Swift?”
  3. 3. Swift Chris Lattner, Swift creator
  4. 4. Swift • Multi-paradigm • Static, inferred typing • Bridged with Objective-C • No pointers
  5. 5. –Someone at WWDC Keynote It’s like “Objective-C without C.”
  6. 6. Swift features • Namespacing* • Generic classes & functions • Named/default parameters • Functions are first class citizens • Optional types
  7. 7. Optionals • Used in situations where value may be absent • Alternative for obj-c nil passing • works with any type
  8. 8. Optionals - (NSInteger)indexOfObject:(id)object;
  9. 9. Optionals func indexOf(object: AnyObject) -> Int?
  10. 10. Optionals • Can be safely unwrapped (if/else) • Can be force unwrapped (runtime exception if value is missing)
  11. 11. 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
  12. 12. Enumerations enum Tree { case Empty case Leaf case Node }
  13. 13. Enumerations enum Tree { case Empty case Leaf(Int) case Node(Tree, Tree) }
  14. 14. Enumerations enum Tree<T> { case Empty case Leaf(T) case Node(Tree, Tree) }
  15. 15. Enumerations let tree: Tree<Int> = .Node(.Leaf(1), .Leaf(1))
  16. 16. Enumerations enum Tree<T> { case Empty case Leaf(T) case Node(Tree, Tree) func depth<T>(t: Tree<T>) -> Int { return 0 } }
  17. 17. Enumerations enum Tree<T> { case … func depth() -> Int { func _depth<T>(t: Tree<T>) -> Int { return 0 } return _depth(self) } }
  18. 18. Enumerations enum Tree<T> { case … func depth() -> Int { func _depth<T>(t: Tree<T>) -> Int { switch t { case .Empty: return 0 } return _depth(self) } }
  19. 19. Enumerations enum Tree<T> { case … func depth() -> Int { func _depth<T>(t: Tree<T>) -> Int { switch t { case .Empty: return 0 case .Leaf(let _): return 1 } return _depth(self) } }
  20. 20. Enumerations enum Tree<T> { case … func depth() -> Int { func _depth<T>(t: Tree<T>) -> 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) } }
  21. 21. Enumerations enum Tree<T> { case Empty case Leaf(T) case Node(Tree, Tree) func depth() -> Int { func _depth<T>(t: Tree<T>) -> 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) } }
  22. 22. Collections • Array, Dictionary, String (contains Char) • Collections are structs • Implicitly bridged to Cocoa collection types
  23. 23. Collections func filter<S : Sequence>(…) -> Bool) -> FilterSequenceView<S> func reverse<C : Collection …>(source: C) -> ReverseView<C> Some of operations are lazy evaluated
  24. 24. Collections func filter<S : Sequence>(…) -> Bool) -> FilterSequenceView<S> func reverse<C : Collection …>(source: C) -> ReverseView<C> Some of operations are lazy evaluated
  25. 25. Built-in immutability var b = 3 b += 1 let a = 3 a += 1 // error
  26. 26. Dictionary immutability let d = ["key0": 0] d["key"] = 3 //error d.updateValue(1, forKey: "key1") //error
  27. 27. Array immutability let c = [1, 2, 3] c[0] = 3 // success c.append(5) // fail
  28. 28. Array immutability let c = [1, 2, 3] c[0] = 3 // success c.append(5) // fail It’s a bug: https://devforums.apple.com/message/971330#971330
  29. 29. Extensions • extends any named type (struct, enum, class) • structures code
  30. 30. Extensions struct Foo { let value : Int } extension Foo : Printable { var description : String { get {return "Foo"} } } extension Foo : Equatable { } func ==(lhs: Foo, rhs: Foo) -> Bool { return lhs.value == rhs.value }
  31. 31. What Swift is missing • Preprocessor • Exceptions • Access control * • KVO, KVC • Compiler attributes (platforms, deprecations, etc.) • performSelector: is unavailable
  32. 32. 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
  33. 33. Objective-C bridging • NSArray < - > Array • NSDictionary < - > Dictionary • NSNumber - > Int, Double, Float
  34. 34. Objective-C bridging @objc class Foo { init (bar: String) { /*...*/ } }
  35. 35. Objective-C bridging @objc(objc_Foo) class Foo { @objc(initWithBar:) init (bar: String) { /*...*/ } }
  36. 36. Objective-C bridging Foo *foo = [[Foo alloc] initWithBar:@"Bar"];
  37. 37. Objective-C bridging func convertPoint(point: CGPoint, toWindow window: UIWindow!) -> CGPoint - (CGPoint)convertPoint:(CGPoint)point toWindow:(UIWindow *)window
  38. 38. Objective-C bridging • All object types are mapped as implicitly unwrapped optionals (T!) • All ‘id’ types are mapped as ‘AnyObject’
  39. 39. Swift internals • Swift objects are Obj-c objects • Implicit root class ‘SwiftObject’ • Ivars type encoding is stored separately • Method’s vtable • Name mangling
  40. 40. Name mangling class Foo { func bar() -> Bool { return false } } _TFC9test3Foo3barfS0_FT_Sb Swift keeps function metadata encoded in function symbols
  41. 41. Performance • 10-100 x slower than C++ (-O0) • 10 x slower than C++ (-O3) • 1 x as C++ (-Ofast)*
  42. 42. Performance • Swift is still in beta • Unoptimized calls of retain/release in loops
  43. 43. Cross-platform code #if os(iOS) typealias View = UView #else typealias View = NSView #endif class MyControl : View { }
  44. 44. Pattern matching let point = (0, 1) if point.0 >= 0 && point.0 <= 1 && point.1 >= 0 && point.1 <= 1 { println("I") } ...
  45. 45. Pattern matching let point = (0, 1) switch point { case (0...1, 0...1): println("I") … }
  46. 46. 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.") }
  47. 47. 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
  48. 48. Auto closures • Wraps function argument in explicit closure func assert(condition:() -> Bool, message: String) { #if DEBUG if !condition() { println(message) } #endif } assert({5 % 2 == 0}, "5 isn't an even number.")
  49. 49. Auto closures Wraps function argument in explicit closure func assert(condition: @auto_closure () -> Bool, message: String) { #if DEBUG if !condition() { println(message) } #endif } assert(5 % 2 == 0, "5 isn't an even number.")
  50. 50. Implicit type conversion struct Box<T> { let _value : T init (_ value: T) { _value = value } } let boxedInt = Box(1) //Box<Int>
  51. 51. Implicit type conversion func foo(i: Int) {…} foo(boxedInt) //error: ’Box<Int>' is not convertible to 'Int'
  52. 52. Implicit type conversion extension Box { @conversion func __conversion() -> T { return value } } foo(boxedInt) //success
  53. 53. Implicit type conversion • allows any type to be ‘nil’ (which has NilType) • allows toll-free-bridging with Cocoa types
  54. 54. Reflection struct Foo { var str = "Apple" let int = 13 func foo() { } } reflect(Foo()).count // 2 reflect(Foo())[0].0 // "str" reflect(Foo())[0].1.summary // "Apple
  55. 55. 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<UInt64>) -> CInt;
  56. 56. Scripting and REPL • xcrun swift - launches REPL • xcrun -i ‘file.swift’ - executes script
  57. 57. Where can I swift? • BDD Testing framework: Quick • Reactive programming: RXSwift • Model mapping: Crust • Handy JSON processing: SwiftyJSON
  58. 58. Thx! @delebedev lebedzeu@yandex-team.ru
  59. 59. Credits • http://nondot.org/sabre/ • https://devforums.apple.com/thread/227288 • http://andelf.github.io/blog/2014/06/08/swift-implicit-type-cast/ • https://www.youtube.com/watch?v=Ii-02vhsdVk • http://www.eswick.com/2014/06/inside-swift/ • http://article.gmane.org/gmane.comp.compilers.clang.devel/37217 • http://stackoverflow.com/questions/24101718/swift-performance-sorting-arrays • http://www.splasmata.com/?p=2798 • https://github.com/rodionovd/SWRoute/wiki/Function-hooking-in-Swift

×