Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
Fashion×AI「似合う」を届けるためのWEARのAI戦略
zozotech
PRO
2
1.1k
SREには開発組織全体で向き合う
koh_naga
0
400
ActiveJobUpdates
igaiga
1
290
Oracle Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
2
170
Bedrock AgentCore Memoryの新機能 (Episode) を試してみた / try Bedrock AgentCore Memory Episodic functionarity
hoshi7_n
2
1.4k
AIBuildersDay_track_A_iidaxs
iidaxs
3
910
AI-DLCを現場にインストールしてみた:プロトタイプ開発で分かったこと・やめたこと
recruitengineers
PRO
2
220
ハッカソンから社内プロダクトへ AIエージェント「ko☆shi」開発で学んだ4つの重要要素
sonoda_mj
6
1.3k
AWSに革命を起こすかもしれない新サービス・アップデートについてのお話
yama3133
0
450
Strands Agents × インタリーブ思考 で変わるAIエージェント設計 / Strands Agents x Interleaved Thinking AI Agents
takanorig
4
1.6k
Snowflake導入から1年、LayerXのデータ活用の現在 / One Year into Snowflake: How LayerX Uses Data Today
civitaspo
0
1.5k
マイクロサービスへの5年間 ぶっちゃけ何をしてどうなったか
joker1007
17
7.3k
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Scaling GitHub
holman
464
140k
Six Lessons from altMBA
skipperchong
29
4.1k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
77
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1k
30 Presentation Tips
portentint
PRO
1
170
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
62
A Modern Web Designer's Workflow
chriscoyier
698
190k
Amusing Abliteration
ianozsvald
0
64
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
2
3.8k
What's in a price? How to price your products and services
michaelherold
246
13k
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) } }