I'd like to create a generic class with type parameter T
which then creates instances of an inner class wrapping values of type T
. I thought that if I used upper bound to tell that T
must be subtype of eg. String
, I'll be able to create instances of the inner class initialized with a String
.
class Test[T <: String] {
private class TestEntry(val value: T)
def init: Unit = {
new TestEntry("")
}
}
But I get the following error:
<console>:12: error: type mismatch;
found : java.lang.String("")
required: T
new TestEntry("")
^
Actually, for my purpose, it would do to omit the upper bound and call new TestEntry(null)
, but the compiler does not even accept null
.
What am I missing?
Edit:
Let me rephrase my example, I need something like this: I should be able to test an abstract luxury car service with any specific car without the need to re-implement the testing method. So both the abstract class (or trait) and the methods should work on subclasses of luxury cars.
class Car
class LuxuryCar extends Car
class Mercedes extends LuxuryCar
class Ferrari extends LuxuryCar
trait LuxuryCarService[CarType <: LuxuryCar] {
def testWithCar(car: CarType): Unit = {}
def testWithARandomLuxuryCar: Unit = test(new Mercedes)
def testWithNoCar: Unit = test(null)
}
trait MercedesCarService extends LuxuryCarService[Mercedes]