Back to Scala3

E040: Expected Token But Found

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

3.8.41.2 KB
Original Source

E040: Expected Token But Found

This error is emitted when the compiler expects a specific token (like a keyword, symbol, or identifier) but finds a different token instead.

This is a general syntax error that indicates the code structure doesn't match what the parser expected at that position. Common causes include missing punctuation, mismatched brackets, or using a keyword where an identifier is expected.


Example

scala
def example(x: Int y: Int) = x + y

Error

scala
-- [E040] Syntax Error: example.scala:1:20 -------------------------------------
1 |def example(x: Int y: Int) = x + y
  |                    ^
  |                    an identifier expected, but ':' found

Solution

scala
// Add the missing comma between parameters
def example(x: Int, y: Int) = x + y
scala
// Or use separate parameter lists
def example(x: Int)(y: Int) = x + y
<!-- 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>