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
Scalaの型クラスを完全に理解する
Search
Jun Tomioka
August 23, 2018
Technology
2.1k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Scalaの型クラスを完全に理解する
Jun Tomioka
August 23, 2018
More Decks by Jun Tomioka
See All by Jun Tomioka
Dotty で軽量な DI ライブラリをかいてみた
jooohn
1
390
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
8k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.2k
Write stack safe non-tailrec recursive functions
jooohn
4
1k
Introduction to Clean Architecture
jooohn
1
620
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.8k
Work at M3 USA
jooohn
0
1.5k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.4k
怖くないCats
jooohn
0
930
Other Decks in Technology
See All in Technology
AgentCore Runtime上にAgentic Coding基盤を構築・展開する際の設計ポイントと限界点 / Design considerations and limitations when building an agentic coding platform on AgentCore Runtime
har1101
5
550
株式会社シーエーシー エンジニア向け会社紹介資料
cac
0
57k
え、こんなに早く改修できるの?──新人エンジニアとスクラムマスターの2人が語る、AI×アジャイル開発の現場
ysasago
2
590
Azure Serverless 2026:Production-ready な AI エージェント基盤 / Azure Serverless 2026: Production-Ready AI Agent Platform
miyake
2
430
SREの視点で考えるSIEM活用術 〜AWS環境でのセキュリティ強化〜
cscengineer
PRO
0
130
積み重なった技術負債への挑戦 〜初手としての全社ゴト化〜
techtekt
PRO
0
1.4k
AI時代の「技術的負債」の変質ー概念の終焉と再解釈、エージェントと共に向かう先
nwiizo
1
3k
C#未経験の僕がAIに読めるコードを書かせるまで
maguroalternative
0
160
現場で役立つ技術負債の効果的な返済方法
masuda220
PRO
9
4.8k
JSONataとAWS Step Functionsで目指すRuntimelessな世界
mu7889yoon
0
340
事業課題から技術的負債に向き合う
sansantech
PRO
2
2.1k
AIエージェントの一手は 誰も見ていない - Falco拡張OSS「Prempti」とeBPFで サーバーレス実行基盤を二層防御する
keitah
0
400
Featured
See All Featured
Un-Boring Meetings
codingconduct
0
420
Designing Powerful Visuals for Engaging Learning
tmiket
1
570
The untapped power of vector embeddings
frankvandijk
2
1.9k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
240
Writing Fast Ruby
sferik
630
63k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Paper Plane (Part 1)
katiecoart
PRO
2
11k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
580
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
530
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
3
4.3k
Designing for Timeless Needs
cassininazir
1
480
Transcript
Scalaの型クラスを 完全に理解する @jooohn1234
M3, Inc. @jooohn1234 じょんと呼んでください!
型クラス (Type Classes)
型クラス is
型のクラス
型クラス is 型のクラス
うんうん、わかるわかる!
None
None
型クラスは 同様の型のひとまとまり
雰囲気 0, 100, -50 Int ??? データ 型 型クラス インスタンス
インスタンス クラス クラス
Wikipedia
“a type class is a type system construct that supports
ad hoc polymorphism. ”
うんうん、わかるわかる!
“... is also known as function overloading or operator overloading.
”
型クラスは 同様の型のまとまりで Function Overloadingを実現す る型システム上の機構
JsonEncoder 型クラスのイメージ • def toJson(value: ???): Json • こんなことができそう ◦
toJson(1) ◦ toJson(“hoge”) ◦ toJson(User(userId = 1, name = “jooohn”))
Scalaでの型クラス
1. 型クラスとなるtraitを宣言
1. 型クラスとなるtraitを宣言 型パラメータで型クラスの インスタンスの型を受け取 るようにする
1. 型クラスとなるtraitを宣言 JsonEncoder[A]の値が 存在するとき AはJsonEncoder型クラ スのインスタンス
2. 型クラスのインスタンスを作成 (implicit)
2. 型クラスのインスタンスを作成 (implicit) StringはJsonEncode型 クラスのインスタンス
3. 型クラスをimplicitに利用するメソッドを定義
3. 型クラスをimplicitに利用するメソッドを定義 JsonEncode[A] 型の値を implicitスコープから参照
4. 値(の型)によっていい感じに処理をoverload
わからないでもない!
それで、subtypingと比べて なにがうれしいの?
これでもいいような
Scalaで型クラスを使うn個の理由 1. finalな型の拡張 2. 値に依存しない関数の定義 3. scopeのコントロール 4. インスタンスの導出 5.
解決する課題の違い
1. finalな型の拡張
1. Final なクラスの拡張 • Scala標準ライブラリなどで定義されている クラスなんかも拡張できる
2. 値に依存しない関数の定義
2. 値に依存しない関数の定義 • JsonDecoderを普通のtraitの使い方では 作れない。(Json => A のシグネチャ)
2. 値に依存しない関数の定義
3. スコープのコントロール
3. scopeのコントロール
4. インスタンスの導出
4. インスタンスの導出 • implicit def を用いた動的なインスタンス生 成
4. インスタンスの導出 • implicit def を用いた動的なインスタンス生 成
4. インスタンスの導出 • (Advanced) shapelessを用いたcase classか らのインスタンス導出
4. インスタンスの導出 • (Advanced) shapelessを用いたcase classか らのインスタンス導出
5. 解決する課題の違い
5. 解決する課題の違い モチベーション 利用時の型 subtyping 複数の型のインスタン スを、抽象型として扱い たい。 abstract: val
strategy: Strategy = SimpleStrategy type classes 任意の型に関して行う 共通処理を提供した い。 concrete: toJson(1: Long)
5. 解決する課題の違い 0, 100, -50 Int JsonEncoder データ 型 型クラス
インスタンス インスタンス クラス クラス subtyping
完全に理解した!