Back to Scala3

E067: Only Classes Can Have Declared But Undefined Members

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

3.8.41.3 KB
Original Source

E067: Only Classes Can Have Declared But Undefined Members

This error is emitted when an abstract member (a declaration without implementation) appears outside of a class or trait context, such as in an object.

Only classes and traits can have abstract members. Objects, being concrete singleton instances, must have all members fully implemented. Variables also need to be initialized to be defined.


Example

scala
object Example:
  def abstractMethod: Int

Error

scala
-- [E067] Syntax Error: example.scala:2:6 --------------------------------------
2 |  def abstractMethod: Int
  |      ^
  |Declaration of method abstractMethod not allowed here: only classes can have declared but undefined members

Solution

scala
// Provide an implementation in objects
object Example:
  def concreteMethod: Int = 42
scala
// Or use a trait or abstract class for abstract members
trait Example:
  def abstractMethod: Int

object ConcreteExample extends Example:
  def abstractMethod: Int = 42
<!-- 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>