Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Bright Future of Swift

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

The Bright Future of Swift

This talk was presented at dotSwift 2016, which took place in Paris on January 29, 2016.

Thomas Visser

January 29, 2016

Transcript

55 slides
  1. I can see clearly now, where Swift will go I

    can see all obstacles in my way Gone are the dark clouds that had me blind It’s gonna be a bright, bright sun-shiny day. - Johnny Nash (emphasis mine)
  2. class Box<T> { let value: T init(_ value: T) {

    self.value = value } } enum Result<T, E> { case Success(Box<T>) case Failure(Box<E>) }
  3. session.dataTaskWithURL(url) { (data, response, error) -> Void in // process

    the response dispatch_async(dispatch_get_main_queue()) { // update UI } } session.delegateQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
  4. func getBirds(completionHandler: [(Bird, UIImage)] -> Void) { session.dataTaskWithURL(NSURL(string: "http://developer.apple.com/naming.json")!) {

    (data, response, error) -> Void in if let data = data, json = self.birdsJsonFromResponseData(data) { var result: [(Bird, UIImage)] = [] for (index, bird) in json.enumerate() { if let bird = self.parseBird(bird) { self.getImage(bird) { image in result.insert((bird, image), atIndex: min(result.count, index)) if result.count == json.count { dispatch_async(dispatch_get_main_queue()) { completionHandler(result) } } } } } } }.resume() }
  5. func getBirds(completionHandler: [(Bird, UIImage)] -> Void) { session.dataTaskWithURL(NSURL(string: "http://developer.apple.com/naming.json")!) {

    (data, response, error) -> Void in if let data = data, json = self.birdsJsonFromResponseData(data) { var result: [(Bird, UIImage)] = [] for (index, bird) in json.enumerate() { if let bird = self.parseBird(bird) { self.getImage(bird) { image in result.insert((bird, image), atIndex: min(result.count, index)) if result.count == json.count { dispatch_async(dispatch_get_main_queue()) { completionHandler(result) } } } } } } }.resume() }
  6. getImage("swift").onSuccess { image in // do something with the image

    }.onSuccess { image in // do something else with the image }
  7. getImage("swift").onSuccess { image in // do something with the image

    }.onSuccess { image in // do something else with the image }.onFailure { error in // handle the error }
  8. func getImage(name: String) -> Future<UIImage, Error> { return Future {

    completion in session.fetchImage(name, completionHandler: { image in completion(.Success(image)) }) } }
  9. getImage("swift").onSuccess { image in // do something with the image

    }.onSuccess { image in // do something else with the image }.onFailure { error in // handle the error }
  10. let image = getImage("swift") // image is a Future<UIImage, Error>

    image.zip(getDetails("swift")).onSuccess { image, details in // do something with the image and the details }
  11. let image = getImage("swift") // image is a Future<UIImage, Error>

    image.zip(getDetails("swift")).onSuccess { image, details in // do something with the image and the details }.onFailure { error in // deal with the error }
  12. getDetails("swift").map { details in return details.substringToIndex( details.startIndex.advancedBy(30) ) }.map {

    summary in return summary + "... Read more" } // subtitle is a Future<String, Error> let subtitle =
  13. getDetails("swift").map { details in return details.substringToIndex( details.startIndex.advancedBy(30) ) }.map {

    summary in return summary + "... Read more" } // subtitle is a Future<String, Error> let subtitle =
  14. func +<E>(lhs: Future<String, E>, rhs: String) -> Future<String, E> getDetails("swift").map

    { details in return details.substringToIndex( details.startIndex.advancedBy(30) ) }.map { summary in return summary + "... Read more" } // subtitle is a Future<String, Error> let subtitle =
  15. func +<E>(lhs: Future<String, E>, rhs: String) -> Future<String, E> {

    return lhs.map { l in return l + rhs } } getDetails("swift").map { details in return details.substringToIndex( details.startIndex.advancedBy(30) ) }.map { summary in return summary + "... Read more" } // subtitle is a Future<String, Error> let subtitle =
  16. getDetails("swift").map { details in return details.substringToIndex( details.startIndex.advancedBy(30) ) } +

    "... Read more" func +<E>(lhs: Future<String, E>, rhs: String) -> Future<String, E> { return lhs.map { l in return l + rhs } } let subtitle =
  17. extension FutureType where Value == String { func substringToIndex(index: Int)

    -> Future<String, Value.Error> { return map { str in return str.substringToIndex(str.startIndex.advancedBy(index)) } } } getDetails("swift").map { details in return details.substringToIndex( details.startIndex.advancedBy(30) ) } + "... Read more" let subtitle =
  18. getDetails("swift").substringToIndex(30) + "... Read more" extension FutureType where Value ==

    String { func substringToIndex(index: Int) -> Future<String, Value.Error> { return map { str in return str.substringToIndex(str.startIndex.advancedBy(index)) } } } let subtitle =
  19. func getImage(url: String) -> async UIImage { let data: async

    NSData = session.fetch(url) return UIImage(data: data) }
  20. func setImage(url: String) -> async Void { let image: async

    UIImage = getImage(url) when let image = image { imageView.image = image } }
  21. func setImage(url: String) -> Future<Void, Error> { let image: Future<UIImage,

    Error> = getImage(url) return image.onSuccess { image in imageView.image = image }.asVoid() }
  22. func setImage(url: String) -> async Void { let image: async

    UIImage = getImage(url) when let image = image { imageView.image = image } }
  23. func setImage(url: String) { let image: async UIImage = getImage(url)

    if let image = image { imageView.image = image } }
  24. @_semantics("swift.concurrent.launch") func async<A>(args: A, task: A -> Void) { //

    launch the task } async(100, fibonacci) The thread-safety layer Copyable Reentrant Code Gateway Annotation

More Decks by Thomas Visser

Other Decks in Programming