Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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[_])?

like image 417
Julien Gaugaz Avatar asked Apr 05 '12 09:04

Julien Gaugaz


People also ask

Can Case classes contain member functions?

Case classes can not contain member functions is the true statement. Case classes are analogous to Java's data POJO.

For which kind of data should you use a case class?

Case classes are good for modeling immutable data. In the next step of the tour, we'll see how they are useful in pattern matching.

What methods get generated when we declare a case class?

equals and hashCode methods are generated, which let you compare objects and easily use them as keys in maps.


1 Answers

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] })
like image 182
Daniel C. Sobral Avatar answered Nov 10 '22 00:11

Daniel C. Sobral