E132: Static Overriding Non-Static Members

This error occurs when a @static member in a companion object tries to override or implement a non-static member from a parent trait or class.

The @static annotation causes a member to be compiled as a JVM static member. Static members cannot participate in inheritance hierarchies because they are not part of instances and cannot be dynamically dispatched. Therefore, a @static member cannot override or implement abstract members from parent types.


Example

import scala.annotation.static

trait Parent:
  def value: Int

class Child

object Child extends Parent:
  @static def value: Int = 42

Error

-- [E132] Syntax Error: example.scala:9:14 -------------------------------------
9 |  @static def value: Int = 42
  |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |  @static members cannot override or implement non-static ones

Solution

// Remove the @static annotation to allow proper implementation
import scala.annotation.static

trait Parent:
  def value: Int

class Child

object Child extends Parent:
  def value: Int = 42
// Alternative: Don't extend the trait and use a different method name
import scala.annotation.static

trait Parent:
  def value: Int

class Child

object Child:
  @static def staticValue: Int = 42