Back to Scala3

E095: Expected Type Bound Or Equals

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

3.8.41.9 KB
Original Source

E095: Expected Type Bound Or Equals

This error is emitted when a type parameter or type member definition has invalid syntax - expecting =, >:, or <: but finding something else.

Type parameters and abstract types may be constrained by type bounds:

  • = for type aliases
  • <: for upper type bounds (subtype constraint)
  • >: for lower type bounds (supertype constraint)

Example

scala
class Container:
  type MyType @

Error

scala
-- [E095] Syntax Error: example.scala:2:14 -------------------------------------
2 |  type MyType @
  |              ^
  |              =, >:, or <: expected, but '@' found
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Type parameters and abstract types may be constrained by a type bound.
  | Such type bounds limit the concrete values of the type variables and possibly
  | reveal more information about the members of such types.
  |
  | A lower type bound B >: A expresses that the type variable B
  | refers to a supertype of type A.
  |
  | An upper type bound T <: A declares that type variable T
  | refers to a subtype of type A.
   -----------------------------------------------------------------------------

Solution

scala
// Use = for type alias
class Container:
  type MyType = Int
scala
// Use <: for upper bound
class Container:
  type MyType <: AnyVal
scala
// Use >: for lower bound
class Container:
  type MyType >: Nothing
<!-- 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>