Back to Scala3

E099: Only Functions Can Be Followed By Underscore

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

3.8.41.4 KB
Original Source

E099: Only Functions Can Be Followed By Underscore

This error is emitted when the eta-expansion syntax x _ is used on an expression that is not a method.

The syntax x _ for converting a method to a function value is deprecated and only works with methods. For non-method expressions, you need to explicitly write () => x to create a function value.


Example

scala
val x = 42
val f = x _

Error

scala
-- [E099] Syntax Error: example.scala:2:10 -------------------------------------
2 |val f = x _
  |        ^^^
  |Only function types can be followed by _ but the current expression has type Int
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The syntax x _ is no longer supported if x is not a function.
  | To convert to a function value, you need to explicitly write () => x
   -----------------------------------------------------------------------------

Solution

scala
// Use explicit function syntax
val x = 42
val f = () => 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>