E233: Illegal Use of Specialized Error

This error occurs when Specialized is used outside of a context bound.


Example

def bar(x: Specialized[Int]): Int = ???   // error: Specialized may only be used as a context bound  
class Foo(x: Specialized[Int])            // error: Specialized may only be used as a context bound
class Bar(val x: Specialized[Int])        // error: Specialized may only be used as a context bound
val y: Specialized[Char] = ???            // error: Specialized may only be used as a context bound
val v = Specialized.apply[Int]            // error: Specialized may only be used as a context bound
def z = println(Specialized.apply[Float]) // error: Specialized may only be used as a context bound
def a = Specialized.apply                 // error: Specialized may only be used as a context bound
type V = Specialized                      // error: Specialized may only be used as a context bound
type W = Specialized[Int]                 // error: Specialized may only be used as a context bound    
val b = Specialized                       // error: Specialized may only be used as a context bound

Error

-- [E233] Syntax Error: example.scala:2:13 -------------------------------------
2 |class Foo(x: Specialized[Int])            // error: Specialized may only be used as a context bound
  |             ^^^^^^^^^^^^^^^^
  |             Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:3:17 -------------------------------------
3 |class Bar(val x: Specialized[Int])        // error: Specialized may only be used as a context bound
  |                 ^^^^^^^^^^^^^^^^
  |                 Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:1:11 -------------------------------------
1 |def bar(x: Specialized[Int]): Int = ???   // error: Specialized may only be used as a context bound  
  |           ^^^^^^^^^^^^^^^^
  |           Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:4:7 --------------------------------------
4 |val y: Specialized[Char] = ???            // error: Specialized may only be used as a context bound
  |       ^^^^^^^^^^^^^^^^^
  |       Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:5:8 --------------------------------------
5 |val v = Specialized.apply[Int]            // error: Specialized may only be used as a context bound
  |        ^^^^^^^^^^^
  |        Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:6:16 -------------------------------------
6 |def z = println(Specialized.apply[Float]) // error: Specialized may only be used as a context bound
  |                ^^^^^^^^^^^
  |                Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:7:8 --------------------------------------
7 |def a = Specialized.apply                 // error: Specialized may only be used as a context bound
  |        ^^^^^^^^^^^
  |        Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:8:5 --------------------------------------
8 |type V = Specialized                      // error: Specialized may only be used as a context bound
  |^^^^^^^^^^^^^^^^^^^^
  |Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:9:5 --------------------------------------
9 |type W = Specialized[Int]                 // error: Specialized may only be used as a context bound    
  |^^^^^^^^^^^^^^^^^^^^^^^^^
  |Specialized may only be used as a context bound
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Specialized allows for a performance improvement by specializing generic type parameters
  |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
  |         generic type in inline traits or inline methods:
  |
  |         inline trait Vec[T: Specialized](val x: T)
  |
  |         inline def foo[T: Specialized](v: Vec[T]) = v.x
  |
  |         In this instance it was used in a way which is unsupported, such as
  |         trying to create a type synonym or a value with explicit type Specialized[X].  
  |       
   -----------------------------------------------------------------------------
-- [E233] Syntax Error: example.scala:10:8 -------------------------------------
10 |val b = Specialized                       // error: Specialized may only be used as a context bound
   |        ^^^^^^^^^^^
   |        Specialized may only be used as a context bound
   |----------------------------------------------------------------------------
   | Explanation (enabled by `-explain`)
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   | Specialized allows for a performance improvement by specializing generic type parameters
   |         to avoid boxing/unboxing. It should only be used as a context (typeclass) bound on a
   |         generic type in inline traits or inline methods:
   |
   |         inline trait Vec[T: Specialized](val x: T)
   |
   |         inline def foo[T: Specialized](v: Vec[T]) = v.x
   |
   |         In this instance it was used in a way which is unsupported, such as
   |         trying to create a type synonym or a value with explicit type Specialized[X].  
   |       
    ----------------------------------------------------------------------------

Solution

Use Specialized correctly, i.e. as a context bound in an inline trait or method:

inline trait Vec[T: Specialized](val x: T)

inline def foo[T: Specialized](v: Vec[T]) = v.x