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

"Subclassing Structs" - dotSwift 2017

"Subclassing Structs" - dotSwift 2017

Presentation from dotSwift 2017 conference, in Paris, Jan 2017.
Vidéo: https://www.dotconferences.com/2017/01/dimitri-dupuis-latour-subclassing-structs

In this lightning talk (4"), I describe a pattern I use in my ‘struct’ objects to have some form of ‘class’ hierarchy, without using ‘classes’. How do you subclass a struct ? Answer: Enums, Associated values and Composition…

Dimitri Dupuis-Latour

January 27, 2017
Tweet

More Decks by Dimitri Dupuis-Latour

Other Decks in Programming

Transcript

  1. 37 000 000X FASTER SWIFT 2.0 - WHOLE MODULE OPTIMIZATION

    struct vs class github.com/knguyen2708/StructVsClassPerformance
  2. struct Post { let date: Date let author: String let

    text: String } let myPost = Post(date: Date(), author: "Dimitri", text: "I'm on stage at dotSwift 2017 !")
  3. struct Post { let date: Date let author: String let

    text: String } let myPost = Post(date: Date(), author: "Dimitri", text: "I'm on stage at dotSwift 2017 !") init(date: Date, author: String, text: String) { self.date = date self.author = author self.text = text } ✗
  4. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let text: String }
  5. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let text: String? let image: UIImage? let imageLegend: String? let videoURL: URL? let videoDuration: TimeInterval? }
  6. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let text: String? let image: UIImage? let imageLegend: String? let videoURL: URL? let videoDuration: TimeInterval? } ✗
  7. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let text: String? let image: UIImage? let imageLegend: String? let videoURL: URL? let videoDuration: TimeInterval? } ✗ 1. Memory
  8. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let text: String? let image: UIImage? let imageLegend: String? let videoURL: URL? let videoDuration: TimeInterval? } ✗ 1. Memory 2. Semantic
  9. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let text: String? let image: UIImage? let imageLegend: String? let videoURL: URL? let videoDuration: TimeInterval? } ✗ 1. Memory 2. Semantic 3. Optional
  10. SUBCLASSING ? class Post {} class TextPost : Post {}

    class ImagePost : Post {} class VideoPost : Post {}
  11. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let text: String? let image: UIImage? let imageLegend: String? let videoURL: URL? let videoDuration: TimeInterval? }
  12. struct Post { let date: Date let author: String let

    likes: Int = 0 let comments: [String] = [] let type: PostType }
  13. let textPost = Post(date: Date(), author: "Dimitri", type: .text(text: "I'm

    on stage!")) let imagePost = Post(date: Date(), author: "Dimitri", type: .image(image: UIImage(), legend: "Vacation picture")) let videoPost = Post(date: Date(), author: "Dimitri", type: .video(url: URL(string: "youtu.be/abc")!, duration: 60))