Slide 1

Slide 1 text

SUBCLASSING STRUCTS Dimitri Dupuis-Latour
 Freelance iOS Dev, Former Apple Engineer
 @dupuislatour dupuislatour@me.com

Slide 2

Slide 2 text

STRUCTS ARE AWESOME

Slide 3

Slide 3 text

Design with struct as the default, fallback to class as last resort

Slide 4

Slide 4 text

1. VALUE SEMANTICS

Slide 5

Slide 5 text

2. SPEED !

Slide 6

Slide 6 text

40X FASTER SWIFT 1.0 struct vs class

Slide 7

Slide 7 text

37 000 000X FASTER SWIFT 2.0 - WHOLE MODULE OPTIMIZATION struct vs class github.com/knguyen2708/StructVsClassPerformance

Slide 8

Slide 8 text

3. AUTOMATIC INITIALIZER

Slide 9

Slide 9 text

struct Post { let date: Date let author: String let text: String }

Slide 10

Slide 10 text

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 !")

Slide 11

Slide 11 text

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 } ✗

Slide 12

Slide 12 text

PROBLEM:
 HOW DO YOU SUBCLASS ?

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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? }

Slide 15

Slide 15 text

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? } ✗

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

SOLUTIONS

Slide 20

Slide 20 text

SUBCLASSING ? class Post {} class TextPost : Post {} class ImagePost : Post {} class VideoPost : Post {}

Slide 21

Slide 21 text

PROTOCOLS ? protocol Textable {} protocol Imagable {} protocol Videoable {}

Slide 22

Slide 22 text

ENUMS ! enum PostType { case text case image case video }

Slide 23

Slide 23 text

ENUMS ! enum PostType { case text(text:String) case image(image:UIImage, legend:String) case video(url: URL, duration: TimeInterval) }

Slide 24

Slide 24 text

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? }

Slide 25

Slide 25 text

struct Post { let date: Date let author: String let likes: Int = 0 let comments: [String] = [] let type: PostType }

Slide 26

Slide 26 text

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))

Slide 27

Slide 27 text

THANKS ! Dimitri Dupuis-Latour
 Freelance iOS Dev, Former Apple Engineer
 @dupuislatour dupuislatour@me.com