Please consider case class Foo[A, B <: List[A]](l: B) { ... } or something akin. In particular, A as well as B need to be available somewhere in the body of Foo.
Is it possible for the compiler to infer A automatically? For example, Foo(List(1,2,3)) fails as the type checker infers A as Nothing. Perhaps there is a way by using type members to solve this problem?
I have that certain feeling that I’m overlooking something embarassingly simple here 😉
EDIT: I just found out that using another type parameter X works just fine, but atm I don’t understand why that is so:
scala> case class Bar[A, B[X] <: List[X]](l: B[A])
defined class Bar
scala> Bar(List(1,2,3))
res11: Bar[Int,List] = Bar(List(1, 2, 3))
Can someone please explain this to me? Is this a unification issue?
EDIT 2: Using [A, B[X] <: List[X]](l: B[A]) can have undesired implications for certain hierarchies (although it’s not really a big deal). More interestingly, I just stumbled across a blog post by Josh Suereth that implicitly shows that [A, B <: List[A]](l: B with List[A]) works just as well… No need for implicits etc.
According to Alexey Romanov, in principle the type could be inferred automatically but simply isn’t at the time being.
So, at least for now, all type parameters that should be inferred have to show up explicitly in the arguments.