E166: 컨텍스트 함수 타입은 확장할 수 없어요

E166: 컨텍스트 함수 타입은 확장할 수 없어요 (Cannot Extend Function)

클래스가 컨텍스트 함수 타입(?=>을 사용하는 함수 타입)을 확장하려 할 때 발생하는 에러예요.

출처: Scala 3 Reference

본문

컨텍스트 함수 타입은 implicit 파라미터를 갖는 함수를 나타내는 특별한 타입이에요. 클래스는 이런 타입을 직접 확장할 수 없어요. implicit 해석(implicit resolution)과 관련된 특별한 의미를 갖고 있기 때문이에요.

Example

class MyContextFunction extends (Int ?=> String)

Error

-- [E166] Syntax Error: example.scala:1:6 --------------------------------------
1 |class MyContextFunction extends (Int ?=> String)
  |      ^
  |      class MyContextFunction cannot extend a context function class

Solution

// Define a regular class with a method that returns the context function
class MyContextFunction {
  def apply: Int ?=> String = summon[Int].toString
}
// Or implement a regular function type
class MyFunction extends (Int => String) {
  def apply(x: Int): String = x.toString
}

더 알아보기

  • 컨텍스트 함수 타입(context function type)에 대한 자세한 내용은 Scala 3 Reference의 contextual abstractions 문서를 참고하세요.