Back to Scala3

E207: Illegal Unroll Placement

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

3.8.41.6 KB
Original Source

E207: Illegal Unroll Placement

This error occurs when the @unroll annotation is used in an invalid location. The @unroll annotation is an experimental feature that can only be applied to method parameters with default values.

The @unroll annotation is used to generate multiple overloaded versions of a method to maintain binary compatibility when adding new parameters with default values.

Example

scala
import scala.annotation.unroll

@unroll
class UnrollClass

Error

scala
-- [E207] Declaration Error: example.scala:4:6 ---------------------------------
4 |class UnrollClass
  |      ^
  |      @unroll is only allowed on a method parameter

Solution

Use the @unroll annotation only on method or constructor parameters that have default values:

scala
import scala.annotation.unroll
import scala.annotation.experimental

@experimental
final class Unrolled(s: String, n: Int = 1, @unroll b: Boolean = true):
  def foo = s + n + b

Note that @unroll has additional restrictions:

  • The method must be effectively final (cannot be overridden)
  • The method cannot be local
  • For case classes, the annotation should be on the class constructor, not on generated methods

This is an experimental feature and requires the -experimental compiler flag.

<!-- 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>