-
typeclass 는 언제 사용하면 좋을까?scala/basic 2021. 6. 9. 10:44
https://w.pitula.me/2017/typeclasses-vs-polymorphism/
Typeclass: when inheritance is not enough
If you ask Wikipedia about polymorphism, it will tell you that it is about “provisioning a single interface to entities of different types”. And this is true(remember that interface here and in whole post means API, not some element that is part of the
w.pitula.me
1. 기존 class 에 새로운 method 를 추가할 때
2. 동일 method 이름으로 다른 방법으로 각각 처리 되어야 할 때
case class Location(url: String, domain: String)
trait Equal[A] {
def eqv(a1: A, a2: A): Boolean
}
url 로 비교한 Equal 과 domain 으로 비교한 Equal 이 각각 필요할 때
3. Json parser 처럼 component 조합이 필요할 때
예) List(Map[String, Int]] 타입 객체를 json 으로 표현한다면, Map -> Json, List -> Json 을 각각 만들어 조합
4. F-bound 의 대안
'scala > basic' 카테고리의 다른 글
Pattern Match 조합 - Email 주소 예제 (0) 2021.07.27 enum 을 사용한 모델링 (0) 2021.07.23 Dark Syntax Sugar (0) 2021.05.23 Future 의 concurrency control (0) 2021.03.28 Scala Learning (0) 2021.03.28