Back to Scala3

E212: Only Fully Dependent Applied Constructor Type

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

3.8.41.8 KB
Original Source

E212: Only Fully Dependent Applied Constructor Type

This warning is emitted when using an applied constructor type (a type that includes constructor arguments) with a class where not all parameters in the first parameter list are marked as tracked.

Applied constructor types are an experimental feature that allows you to express more precise types by including constructor arguments in the type itself. However, this feature only works correctly when all parameters in the first parameter list are tracked, meaning their values are part of the type.


Example

scala
import scala.language.experimental.modularity

class MyBox(val value: Int)

def example =
  val box: MyBox(42) = MyBox(42)
  box

Error

scala
-- [E212] Type Warning: example.scala:6:11 -------------------------------------
6 |  val box: MyBox(42) = MyBox(42)
  |           ^^^^^^^^^
  |Applied constructor type can only be used with classes where all parameters in the first parameter list are tracked

Solution

Mark the class parameter as tracked to enable applied constructor types:

scala
import scala.language.experimental.modularity

class MyBox(tracked val value: Int)

def example =
  val box: MyBox(42) = MyBox(42)
  box

Alternatively, remove the applied constructor type syntax if you don't need the precise type tracking:

scala
import scala.language.experimental.modularity

class MyBox(val value: Int)

def example =
  val box: MyBox = MyBox(42)
  box
<!-- 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>