E172: Missing Implicit Argument — 빠진 암시적 인자
E172: Missing Implicit Argument — 빠진 암시적 인자
메서드가 암시적(컨텍스트) 파라미터를 요구하는데, 그에 맞는 given 인스턴스가 스코프에 없을 때 발생하는 에러예요.
본문
컨텍스트 파라미터(using 구문이나 예전의 implicit 구문)는 매칭되는 given 인스턴스가 있을 때 컴파일러가 자동으로 채워줘요. 아무것도 찾지 못하면 이 에러가 보고돼요.
예시 (Example)
trait Show[T] {
def show(t: T): String
}
def printIt[T: Show](t: T) = println(summon[Show[T]].show(t))
def test = printIt(42)
에러 (Error)
-- [E172] Type Error: example.scala:7:22 ---------------------------------------
7 |def test = printIt(42)
| ^
|No given instance of type Show[Int] was found for a context parameter of method printIt
해결 방법 (Solution)
trait Show[T] {
def show(t: T): String
}
def printIt[T: Show](t: T) = println(summon[Show[T]].show(t))
// Provide a given instance for the required type
given Show[Int] with {
def show(t: Int) = t.toString
}
def test = printIt(42)
더 알아보기
필요한 타입에 대한 given 인스턴스를 만들어 스코프에 넣어두면 돼요.