Back to Scala3

E214: Illegal Context Bounds

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

3.8.41.5 KB
Original Source

E214: Illegal Context Bounds

This error is emitted when context bounds are used in a position where they are not allowed.

Context bounds (like T: Ordering) are a shorthand for requiring an implicit instance of a type class for a type parameter. However, they can only be used in certain positions, such as method or class type parameters, not in type alias definitions.


Example

scala
trait Equatable[T]

type Foo[T: Equatable]

Error

scala
-- [E214] Syntax Error: example.scala:3:21 -------------------------------------
3 |type Foo[T: Equatable]
  |            ^^^^^^^^^
  |            Context bounds are not allowed in this position

Solution

Context bounds are allowed in method or class definitions where implicit parameters can be added:

scala
trait Equatable[T]

def foo[T: Equatable](value: T): Unit = ()
scala
trait Equatable[T]

class Foo[T: Equatable](value: T)

For type aliases, you cannot use context bounds. If you need to express a constraint, consider using a class or trait instead:

scala
trait Equatable[T]

trait Foo[T]:
  given Equatable[T] = ???
<!-- 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>