scalatypestype-boundstype-constructor

Underscores in type bounds on type constructors


Can someone explain why the following doesn't compile? I want that BB[A] is also a List[A]. The method body only enforces this view.

scala> def x[A, BB[_] <: List[_]](p: BB[A]) {p: List[A]}
<console>:8: error: type mismatch;
 found   : BB[A]
 required: List[A]

       def x[A, BB[_] <: List[_]](p: BB[A]) {p: List[A]}
                                             ^

Solution

  • I think you need to name the _ parameter.

    scala> def x[A, BB[X] <: List[X]](p: BB[A]) {p: List[A]}
    

    works.