Back to Scala3

E083: Not A Path

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

3.8.41.6 KB
Original Source

E083: Not A Path

This error is emitted when an expression is used where a stable path is required, but the expression is not an immutable path.

An immutable path is:

  • A reference to an immutable value (val), or
  • A reference to this, or
  • A selection of an immutable path with an immutable value

Paths are required in certain contexts like singleton types, type projections, and some pattern matches.


Example

scala
var x = 1
def example: x.type = x

Error

scala
-- [E083] Type Error: example.scala:2:13 ---------------------------------------
2 |def example: x.type = x
  |             ^^^^^^
  |(x : Int) is not a valid singleton type, since it is not an immutable path
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | An immutable path is
  |  - a reference to an immutable value, or
  |  - a reference to `this`, or
  |  - a selection of an immutable path with an immutable value.
   -----------------------------------------------------------------------------

Solution

scala
// Use val instead of var for stable paths
val x = 1
def example: x.type = x
scala
// Or don't use singleton types for mutable values
var x = 1
def example: Int = x
<!-- 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>