Back to Scala3

E154: Anonymous Instance Cannot Be Empty

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

3.8.41.3 KB
Original Source

E154: Anonymous Instance Cannot Be Empty

This error occurs when an anonymous given instance is defined without implementing a type or providing at least one extension method. Anonymous givens must either implement a specific type or contain extension methods. An empty anonymous given has no practical use and is not allowed.


Example

scala
given {}

Error

scala
-- [E154] Syntax Error: example.scala:1:6 --------------------------------------
1 |given {
  |      ^
  |      anonymous instance must implement a type or have at least one extension method

Solution

scala
trait Printable {
  def print(): Unit
}

// Implement a type
given Printable {
  def print(): Unit = println("hello")
}

Note: This error code is difficult to reproduce with current parser syntax, as the parser typically reports syntax errors before reaching this validation. The behavior described here reflects the intended semantics of the error message.

<!-- 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>