Implicit value class exposes its underlying value as a public val

ID

scala.correctness.leaking_implicit_class_val

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Api Design

Language

Scala

Description

Reports an implicit class …​ extends AnyVal whose constructor parameter is declared val rather than private val, publishing an accessor for the wrapped value on every type the implicit conversion applies to.

Rationale

An implicit class is in scope for every value of the wrapped type, so a public accessor on it becomes an extension method on that type — "text".self compiles anywhere the conversion is imported. The name is chosen for the wrapper’s internals, not as API, so it collides with real members and shows up in completion on a type the author does not own. The accessor also forces the value class to box wherever it is called, losing the allocation-free property that is the reason to extend AnyVal.

The following code illustrates the pattern detected by this rule:

object Syntax {
  // FLAGGED: Implicit value class exposes its underlying value as a public val
  implicit class RichString(val self: String) extends AnyVal {
    def shout: String = self.toUpperCase
  }
}

Remediation

Declare the parameter private val: implicit class RichString(private val self: String) extends AnyVal. The class body can still use it, and no accessor is added to the wrapped type.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved