Back to Scala3

E034: Existential Types No Longer Supported

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

3.8.42.2 KB
Original Source

E034: Existential Types No Longer Supported

This error is emitted when existential types syntax (using forSome) is used. Existential types are no longer supported in Scala 3.

Existential types were a feature in Scala 2 that allowed abstracting over unknown types using the forSome syntax. In Scala 3, this feature has been removed in favor of simpler and more principled alternatives.

Instead of existential types, you should use:

  • Wildcard types: Use ? (or _ in Scala 2 compatibility mode) for unknown type parameters
  • Type parameters: Use generic methods or classes with explicit type parameters
  • Dependent types: For more advanced use cases

Example

scala
def example[T]: List[T forSome { type T }] = List()

Error

scala
-- [E034] Syntax Error: example.scala:1:23 -------------------------------------
1 |def example[T]: List[T forSome { type T }] = List()
  |                       ^^^^^^^
  |                       Existential types are no longer supported -
  |                       use a wildcard or dependent type instead
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The use of existential types is no longer supported.
  |
  | You should use a wildcard or dependent type instead.
  |
  | For example:
  |
  | Instead of using forSome to specify a type variable
  |
  | List[T forSome { type T }]
  |
  | Try using a wildcard type variable
  |
  | List[?]
   -----------------------------------------------------------------------------

Solution

scala
// Use a wildcard type
def example: List[?] = List()
scala
// Alternative: use a type parameter
def example[T]: List[T] = List()
scala
// Alternative: use Any if the type doesn't matter
def example: List[Any] = List()
<!-- 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>