Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Keep Calm and Type Erase On
Search
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
Transcript
35 slides
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) } }
More Decks by Gwendolyn Weston
See All by Gwendolyn Weston
While Your App is Sleeping
gwengrid
3
670
Other Decks in Technology
See All in Technology
gradle-best-practices 使ってみた
mikanichinose
0
230
20261113_JAWS_Osaka
takuyay0ne
1
610
メルカリの小規模チーム「Pod」で、 エンジニアがデータアナリストなしで、 AIを使ってデータ分析の自走を目指す取り組み
jackojacko_
1
350
HTTP通信に使おうとすると実はめんどくさいfile_get_contents
maguroalternative
0
260
Kansai WordPress Meetup@大阪『ブロックテーマを作る上で知っておくべきこと
torounit
0
310
クラウドでも使えるNetApp -現場目線での設計とクラウドバックアップの活用
ryutakondo
1
320
kubellの最新データ分析環境
kubell_hr
2
260
自動計装なしでECSサービスをApplication Signalsに載せるまで
ota1022
2
470
20261113_AWS Organizations運用を下支えするCFn StackSetsの柔軟性
dkduppf
0
120
「正しいデータ」の定義をAI エージェントとつくるGoogleADKによるデータ品質定義の実践
takaha4k
0
130
「個社」ではなく「業界」の課題を解く AI 対話開発 ー 価値を広げる設計と、価値を守るエンジニアリング ー
ivry_presentationmaterials
4
860
IoTのデータ保存をSREの観点で考える
grimoh
1
120
Featured
See All Featured
Darren the Foodie - Storyboard
khoart
PRO
5
4.1k
Prompt Engineering for Job Search
mfonobong
0
600
Facilitating Awesome Meetings
lara
57
7.2k
BBQ
matthewcrist
90
10k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
540
Rails Girls Zürich Keynote
gr2m
96
14k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
570
A designer walks into a library…
pauljervisheath
211
25k
Abbi's Birthday
coloredviolet
5
11k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
910
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
630