Back to Scala3

E018: Illegal Start Simple Expr

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

3.8.41.8 KB
Original Source

E018: Illegal Start Simple Expr

This error is emitted when the compiler expects an expression but finds a token that cannot start an expression.

An expression cannot start with certain tokens like keywords used in other contexts, or when the expression is missing entirely. This commonly happens when:

  • An expression is incomplete (missing the else branch, missing operand, etc.)
  • A keyword is used where an expression is expected
  • There's a syntax error that confuses the parser

Example

scala
val `given` = 2
def example = if true then 1 else given

Error

scala
-- [E018] Syntax Error: example.scala:2:34 -------------------------------------
2 |def example = if true then 1 else given
  |                                  ^^^^^
  |                                  expression expected but given found
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | An expression cannot start with given.
   -----------------------------------------------------------------------------

Solution

scala
// If using identifiers that are a keywords use backticks
val `given` = 2
def example = if true then 1 else `given`

scala
// Ensure all branches have expressions
val result =
  if true then
    "yes"
  else
    "no"
scala
// Check for missing operands
val sum = 1 + 2  // not: val sum = 1 +
<!-- 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>