Back to Scala3

E035: Unbound Wildcard Type

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

3.8.42.3 KB
Original Source

E035: Unbound Wildcard Type

This error is emitted when the wildcard type syntax (_ or ?) is used in a position where it cannot be bound to a concrete type.

Replace the wildcard with a non-wildcard type. If the type doesn't matter, try replacing the wildcard with Any.

This typically happens in:

  • Parameter lists: def foo(x: _) = ...
  • Type arguments in constructors: val foo = List[?](1, 2)
  • Type bounds: def foo[T <: _](x: T) = ...
  • val and def types: val foo: _ = 3

Example

scala
def example(x: ?) = x

Error

scala
-- [E035] Syntax Error: example.scala:1:15 -------------------------------------
1 |def example(x: ?) = x
  |               ^
  |               Unbound wildcard type
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The wildcard type syntax (_) was used where it could not be bound.
  | Replace _ with a non-wildcard type. If the type doesn't matter,
  | try replacing _ with Any.
  |
  | Examples:
  |
  | - Parameter lists
  |
  |   Instead of:
  |     def foo(x: _) = ...
  |
  |   Use Any if the type doesn't matter:
  |     def foo(x: Any) = ...
  |
  | - Type arguments
  |
  |   Instead of:
  |     val foo = List[?](1, 2)
  |
  |   Use:
  |     val foo = List[Int](1, 2)
  |
  | - Type bounds
  |
  |   Instead of:
  |     def foo[T <: _](x: T) = ...
  |
  |   Remove the bounds if the type doesn't matter:
  |     def foo[T](x: T) = ...
  |
  | - val and def types
  |
  |   Instead of:
  |     val foo: _ = 3
  |
  |   Use:
  |     val foo: Int = 3
   -----------------------------------------------------------------------------

Solution

scala
// Use Any if the type doesn't matter
def example(x: Any) = x
scala
// Use a type parameter for generic behavior
def example[T](x: T): T = x
scala
// Specify a concrete type
def example(x: Int) = 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>