Reputation: 1303
I have this interface with this generic method:
interface IInterface {
fun <T> test(body: T)
}
I want to implement this interface like this:
class MyClass: IInterface {
override fun <JsonObject> test(body: JsonObject) {
if (body is com.google.gson.JsonObject) {
}
}
}
My problem here is type JsonObject not recognized like "com.google.gson.JsonObject". So i can write this code without error in my compilator (intelliJ).
override fun <NotExistingClass__> test(body: NotExistingClass__) {
So, how to define the type of T for JsonObject from Gson ? this code does not work:
override fun <com.google.gson.JsonObject> test(body: com.google.gson.JsonObject)
Thank's
Upvotes: 1
Views: 219
Reputation: 30528
This interface
:
interface IInterface {
fun <T> test(body: T)
}
is not generic, but it has a generic method. If you want to make it generic do this:
interface IInterface<T> {
fun test(body: T)
}
then your implementation will look like this:
class MyClass: IInterface<JsonObject> {
override fun test(body: JsonObject) {
if (body is com.google.gson.JsonObject) {
}
}
}
If you still want a generic method for some reason you'll have to pass the generic type parameter at each call site:
someInstance.test<JsonObject>(obj)
Upvotes: 2