open class CarBaseRepo<in T> : BaseRepo<T>() {
override fun save(t: T): T {
println("Saving object")
return t
}
}
This doesn’t compile because the compiler needs to warrant type safety.
You can refer to this page for a detailed explanation.
A short version is that, when you mark T
as in
, you say that the class is a “consumer” of T.
consider the following interface:
interface Consumer<in T> {
fun accept(T val)
}
Because I marked the type parameter T
as in
, the below assignment is valid:
val consumer: Consumer<Object> = { println(it) }
val stringConsumer: Consumer<String> = consumer
in this example, Consumer<Object>
can be assigned to a Consumer<String>
, because Consumer<String>
is considered a superclass of Consumer<Object>
.
In Java, this would have to be written like so:
Consumer<Object> consumer = System.out::println;
Consumer<? super String> stringConsumer = consumer;
Note the use of wildcard types: <? super String>
. In kotlin, this declaration is moved to the declaration of the type parameter.
This can be done the other way around using out
parameter, so you get the following (producer):
interface Supplier<out T> {
fun get(): T
}
// In kotlin, you can assign Supplier<String> to Supplier<Object> because of "out"
val stringSupplier: Supplier<String> = { "some string" }
val supplier: Supplier<Object> = stringSupplier
// In java, it looks like this:
Supplier<String> stringSupplier = () -> "some string";
Supplier<? extends Object> supplier = stringSupplier;
In your case of CarBaseRepo
,
you’re using the type parameter in both consumer and producer positions:
override fun save(t: T): T
This means that, if it’s marked in
, the type T
can not be returned from any of the functions.
This is because in
means we can go from CarBaseRepo<Object>
to CarBaseRepo<String>
,
but if we return T
from our function save
, we are returning Object
from save but Object
is not assignable to String
.
The solution?
use in
nor out
.
open class CarBaseRepo<T> : BaseRepo<T>() {
override fun save(t: T): T {
println("Saving object")
return t
}
}