How to define case classes with members with unbound type parameters?

◇◆丶佛笑我妖孽 提交于 2019-12-19 18:27:30

问题


Given a class definition with bound type parameter Animal[A <: String] it seems that the Scala compiler does not infer B <: String from Animal[B]. Is the inference allowed? How to help the compiler to do the inference?

Below is a concrete example with case classes where the lack of this inference is a problem.

Consider the following case class hierarchy:

sealed trait Person[+T <: Person[T]]
case class Student() extends Person[Student]
case class Professor() extends Person[Professor]

I need to define a case class University which I can instantiate with a variable of type Person[_], for example val p: Person[_] = Student(). I thought this would work with the following definition:

case class University(p: Person[_])

But this fails compiling with the error:

type arguments [Any] do not conform to trait Person's type parameter bounds [+T <: Person[T]]

If I bind the type parameter of the case class University it compiles (it also compiles with unbounded parameters if i drop the case keyword but this is not an option in my case):

case class BoundUniversity[P <: Person[P]](p: Person[P])

But this parametrized version cannot be instantiated with an unbounded variable of type Person[_]:

val p: Person[_] = Student()
BoundUniversity(p)

fails compiling with:

inferred type arguments [_$1] do not conform to method apply's type parameter bounds [P <: Person[P]]

The same error happens for a method with a bound argument like:

def general[P <: Person[P]](p: P) = println(p)

so this is not specific to class constructors.

Two questions:

  1. The type Person is defined with parameter bounds Person[+T <: Person[T]], so that each instance of this type is insured to respect those bounds: val p: Person[P] implies that P <: Person[P]; or am I missing something? So how can I make this clear to the compiler so that it doesn't complain?

  2. How/Can I define a case class with members with unbound type parameter like case class University(p: Person[_])?


回答1:


A type X[_] is hardly ever what you want. When you use _ on a type, you are basically saying you don't care what that parameter is, because you'll never need to use it.

Anyway, this compiles. It may well bite you down the road, existential types being the tricky stuff that they are, but...

case class University(p: Person[t] forSome { type t <: Person[t] })


来源:https://stackoverflow.com/questions/10025815/how-to-define-case-classes-with-members-with-unbound-type-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!