E017: Unbound Placeholder Parameter
This error is emitted when the underscore (_) placeholder syntax is used in a context where it cannot be bound to a parameter. Consider explicitly writing the variable binding.
This can be done by replacing _ with a variable (eg. x) and adding x => where applicable.
Common incorrect uses:
- Using
_in avaldefinition instead of in a lambda - Using
_outside of a function context - Using
_without a type annotation where type cannot be inferred
Example
val x = _
Error
-- [E017] Syntax Error: example.scala:1:8 --------------------------------------
1 |val x = _
| ^
| Unbound placeholder parameter; incorrect use of _
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| The _ placeholder syntax was used where it could not be bound.
| Consider explicitly writing the variable binding.
|
| This can be done by replacing _ with a variable (eg. x)
| and adding x => where applicable.
|
| Example before:
|
| { _ }
|
| Example after:
|
| x => { x }
|
| Another common occurrence for this error is defining a val with _:
|
| val a = _
|
| But this val definition isn't very useful, it can never be assigned
| another value. And thus will always remain uninitialized.
| Consider replacing the val with var:
|
| var a = _
|
| Note that this use of _ is not placeholder syntax,
| but an uninitialized var definition.
| Only fields can be left uninitialized in this manner; local variables
| must be initialized.
|
| Another occurrence for this error is self type definition.
| The _ can be replaced with this.
|
| Example before:
|
| trait A { _: B => ...
|
| Example after:
|
| trait A { this: B => ...
-----------------------------------------------------------------------------
Solution
// Use an explicit lambda with a named parameter
val f: Int => Int = x => x + 1
// The placeholder syntax works in lambda contexts with clear types
val f: Int => Int = _ + 1
// For uninitialized fields, use var (only in classes, not local scope)
class Example:
var x: Int = scala.compiletime.uninitialized // Uninitialized field, defaults to 0
// Use wildcard in pattern matching
val list = List(1, 2, 3)
val head = list match
case h :: _ => h
case _ => 0
In this article