E126: Value Class Parameter May Not Be Call-By-Name
This error occurs when a value class (a class that extends AnyVal) has a call-by-name parameter (using => T syntax).
Value classes are a special mechanism in Scala that allows defining wrapper classes that avoid runtime allocation overhead. The JVM represents value class instances as their underlying value at runtime. Call-by-name parameters, which are evaluated lazily each time they are accessed, are incompatible with this optimization because they cannot be unboxed to a primitive value.
Example
class Wrapper(x: => Int) extends AnyVal
Error
-- [E126] Syntax Error: example.scala:1:14 -------------------------------------
1 |class Wrapper(x: => Int) extends AnyVal
| ^
| Value class parameter `x` may not be call-by-name
Solution
// Use a regular (by-value) parameter
class Wrapper(val x: Int) extends AnyVal
// Alternative: If lazy evaluation is needed, consider using a regular class
class Wrapper(x: => Int):
def value: Int = x
In this article