Back to Scala3

E081: Anonymous Function Missing Parameter Type

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

3.8.41.3 KB
Original Source

E081: Anonymous Function Missing Parameter Type

This error is emitted when the compiler cannot infer the type of a parameter in an anonymous function (lambda) and no explicit type is provided.

The compiler needs to know the types of function parameters to properly type-check the function body. When there's not enough context to infer the type, you must provide an explicit type annotation.


Example

scala
val f = (x) => x + 1

Error

scala
-- [E081] Type Error: example.scala:1:9 ----------------------------------------
1 |val f = (x) => x + 1
  |         ^
  |         Missing parameter type
  |
  |         I could not infer the type of the parameter x

Solution

scala
// Add an explicit type annotation to the parameter
val f = (x: Int) => x + 1
scala
// Or provide type context through the variable declaration
val f: Int => Int = x => x + 1
scala
// Or use the function in a context that provides type information
val result = List(1, 2, 3).map(x => x + 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>