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

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

Error

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

// If using identifiers that are a keywords use backticks
val `given` = 2
def example = if true then 1 else `given`
// Ensure all branches have expressions
val result =
  if true then
    "yes"
  else
    "no"
// Check for missing operands
val sum = 1 + 2  // not: val sum = 1 +