Back to Scala3

E074: Value Classes May Not Be Abstract

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

3.8.41.2 KB
Original Source

E074: Value Classes May Not Be Abstract

This error is emitted when a value class (a class extending AnyVal) is declared as abstract.

Value classes must be concrete because they are meant to be inlined by the compiler. An abstract value class cannot be instantiated and therefore cannot serve its purpose of avoiding object allocation.


Example

scala
abstract class Wrapper(val value: Int) extends AnyVal

Error

scala
-- [E074] Syntax Error: example.scala:1:15 -------------------------------------
1 |abstract class Wrapper(val value: Int) extends AnyVal
  |               ^
  |               Value classes may not be abstract

Solution

scala
// Remove the abstract modifier
class Wrapper(val value: Int) extends AnyVal
scala
// For abstraction, use a universal trait
trait Numeric extends Any:
  def value: Int

class Wrapper(val value: Int) extends AnyVal with Numeric
<!-- 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>