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
Gwendolyn Weston
March 03, 2016
Technology
3
25k
Keep Calm and Type Erase On
Try!Swift 2016
Gwendolyn Weston
March 03, 2016
Tweet
Share
More Decks by Gwendolyn Weston
See All by Gwendolyn Weston
While Your App is Sleeping
gwengrid
3
650
Other Decks in Technology
See All in Technology
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
1
200
Databricks Free Editionで始めるLakeflow SDP
taka_aki
0
150
Web Intelligence and Visual Media Analytics
weblyzard
PRO
1
6.8k
SOC2は、取った瞬間よりその後が面白い
3flower
0
120
持続可能な開発のためのミニマリズム
sansantech
PRO
3
370
WebDriver BiDi 2025年のふりかえり
yotahada3
1
380
CQRS/ESになぜアクターモデルが必要なのか
j5ik2o
0
1.3k
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1k
Hardware/Software Co-design: Motivations and reflections with respect to security
bcantrill
1
210
ドメイン駆動セキュリティへの道しるべ
pandayumi
0
150
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
これまでのネットワーク運用を変えるかもしれないアプデをおさらい
hatahata021
4
230
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.6k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
150
Optimizing for Happiness
mojombo
379
71k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
2.9k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
0
250
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.8k
Everyday Curiosity
cassininazir
0
120
HDC tutorial
michielstock
1
330
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
61
48k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.5k
Technical Leadership for Architectural Decision Making
baasie
0
220
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) } }