Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Keep Calm and Type Erase On
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Gwendolyn Weston
March 03, 2016
Technology
25k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Keep Calm and Type Erase On
Try!Swift 2016
Gwendolyn Weston
March 03, 2016
More Decks by Gwendolyn Weston
See All by Gwendolyn Weston
While Your App is Sleeping
gwengrid
3
660
Other Decks in Technology
See All in Technology
RSA暗号を手計算したくなること、ありますよね?? (20260615_orestudy6_rsa)
thousanda
0
160
Building applications in the Gemini API family.
line_developers_tw
PRO
0
2.8k
中期計画、2回作ってみた ~業務委託と正社員、両方の視点から~
demaecan
1
640
2026TECHFRESH畢業分享會 - 葬送的通靈師:化系統與用戶雜訊成行動訊號
line_developers_tw
PRO
0
670
Disciplined Vibes: Scaling AI-Assisted Engineering
sheharyar
0
120
LLMにもCAP定理があるという話
harukasakihara
0
280
日本 Fintech 未来予測レポート 2027〜2028年(手動編集版)
8maki
0
1.2k
新規事業を牽引する技術選定 〜フルスタックTypeScript開発の実践事例〜
nullnull
3
380
2026TECHFRESH畢業分享會 - Lightning Talk - 資料也要 CI/CD? 用 Airbyte 自動化資料同步
line_developers_tw
PRO
0
660
価格.comをAI駆動で全面刷新する ー 30年分の技術的負債を返し、次の30年の土台をつくる ー / AI Engineering Summit Tokyo 2026
tkyowa
53
59k
2026TECHFRESH畢業分享會 - AI 時代的人生存檔點
line_developers_tw
PRO
0
670
FDE という解 ― 暗黙知と明示知をつなぐ、伴走型エンジニアリング ―
otanet
0
130
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
698
190k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Facilitating Awesome Meetings
lara
57
7k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
730
The Invisible Side of Design
smashingmag
302
52k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
290
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
610
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
190
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
Tell your own story through comics
letsgokoyo
1
950
Transcript
Keep Calm and Type Erase On Gwendolyn Weston @purpleyay
Why would you want to erase a type? !
What does it mean to erase a type? !
What is a type? !
A type is a classification that defines: • a set
of values • and the legal operations on them
Example: Int Values: all integers in [-∞, ∞] Operations: ➕,
✖ , %
Compilers ! Types
Compilers ! (Some) Types
Two kinds of types ! Concrete types ! Abstract types
"
Concrete types ! A type with unambiguous implementation; can be
instantiated directly.
Concrete types are representations of data.
let concreteInt = 42 let concreteArray = ["much", "concrete", "wow"]
Abstract types ! A type with incomplete implementation; cannot be
instantiated directly
Abstract types are representations of behaviour.
class GenericClass<T> { ... } let object: GenericClass<T> // !
struct GenericStruct<U> { ... } let object: GenericStruct<U> // !
How to make abstract types concrete?
Type reification Making an abstract type concrete by filling in
its placeholder types
Type parameters <T>, <U>
! + <T> = " class GenericClass<T> { ... }
let StringClass: GenericClass<String> // ! struct GenericStruct<T> { ... } let IntStruct: GenericStruct<Int> // !
One exception
protocol + <T> = ! We cannot use type parameters
for generic protocols
protocol Pokemon { typealias PokemonType func attack(move:PokemonType) } class Pikachu:
Pokemon { func attack(move: Electric) { ⚡ } } class Charmander: Pokemon { func attack(move: Fire) { " } }
let pokemon: Pokemon pokemon.attack(selectedAttack)
⾠! let pokemon: Pokemon Protocol Pokemon can only be used
as a generic constraint because it has Self or associated type requirements
AnyPokemon class AnyPokemon <PokemonType>: Pokemon { required init<U:Pokemon where U.PokemonType
== PokemonType>(_ pokemon: U) { } }
let p1 = AnyPokemon(Pikachu()) let p2: AnyPokemon<Fire> p2 = AnyPokemon(Charmander())
let digimon = AnyPokemon(NotAPokemon()) //!
let pokemon = Pikachu() vs. let pokemon: AnyPokemon <Electric> pokemon
= AnyPokemon(Pikachu()) Pikachu is instantiated as type AnyPokemon <Electric>
Type erasure
Type erasure scaffolding?
let pokemon: AnyPokemon <AnyObject> pokemon = AnyPokemon(Pikachu()) //!
No covariance Swift does not allow <Electric> to substitute <AnyObject>
Downsides • Boilerplate • Lost type information • No covariance
Recap • What is a type? • 2 kinds of
types: concrete, abstract • Reification with type parameters • Type erasure
Thanks! @purpleyay
Example class AnyPokemon <PokemonType>: Pokemon { private let _attack: ((PokemonType)
-> Void) required init<U:Pokemon where U.PokemonType == PokemonType>(_ pokemon: U) { _attack = pokemon.attack } func attack(type:PokemonType) { return _attack(type) } }