How to define case classes with members with unbou

2020-04-05 17:24发布

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条回答
Deceive 欺骗
2楼-- · 2020-04-05 18:13

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] })
查看更多
登录 后发表回答