Back to Scala3

E166: Cannot Extend Function

docs/_docs/reference/error-codes/E166.md

3.8.41.3 KB
Original Source

E166: Cannot Extend Function

This error occurs when a class attempts to extend a context function type (a function type using ?=>).

Context function types are special types that represent functions with implicit parameters. Classes cannot directly extend these types because they have special semantics related to implicit resolution.


Example

scala
class MyContextFunction extends (Int ?=> String)

Error

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

Solution

scala
// Define a regular class with a method that returns the context function
class MyContextFunction {
  def apply: Int ?=> String = summon[Int].toString
}
scala
// Or implement a regular function type
class MyFunction extends (Int => String) {
  def apply(x: Int): String = x.toString
}
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>