Type mismatch: why there is .Companion? - kotlin

I wrote the following function:
fun <T> myFunction(res: Response, type: T): T {
return res.`as`(type!!::class.java)
}
And I would like to use it in the following way:
fun anotherFunction(): MyClass {
val res = getResponse()
return myFunction(res, MyClass)
}
But I get the following error:
Type mismatch.
Required: MyClass
Found: MyClass.Companion
How can I solve it?

I don't know exactly what the as function does, but I suppose it performs some sort of conversion from one class instance into an instance of some other class?
You seem to be trying to pass a class type as a function argument. You can do it in one of two ways:
fun <T: Any> myFunction(res: Response, type: Class<T>): T {
return res.`as`(type)
}
fun anotherFunction(): MyClass {
val res = getResponse()
return myFunction(res, MyClass::class.java)
}
or
inline fun <reified T: Any> myFunction(res: Response): T {
return res.`as`(T::class.java)
}
fun anotherFunction(): MyClass {
val res = getResponse()
return myFunction(res) // <MyClass> is inferred by anotherFunction return type
}

Related

Deserialize generic object using Kotlin Serialization

I am trying to replace Gson library by kotlin serialization to handle JSON serialization/deserialization.
I am facing some issues to deserialize generic objects I have setup a simple example of what I am trying to achieve:
#Serializable
data class ContentMessageDto<T>(
val method: String,
val content: T
)
#Serializable
private data class DummyObjectNonNullProperties(
val value: Int,
#SerialName("aaa") val someProp: String,
val bbb: Boolean,
val ccc: Double
)
interface MyParser {
fun <T> parseContentMessage(
json: String
): ContentMessageDto<T>
}
class MyParserImpl(private val jsonSerialization: Json) : MyParser {
override fun <T> parseContentMessage(json: String): ContentMessageDto<T> {
return jsonSerialization.decodeFromString<ContentMessageDto<T>>(json)
}
}
fun main() {
println("start processing...")
val jsonToParse = """
{
"method":"get",
"content":{
"value":345,
"aaa": "some string",
"bbb": true,
"ccc": 23.4
}
}""".trimIndent()
val parser:MyParser = MyParserImpl(Json)
val result = parser.parseContentMessage<DummyObjectNonNullProperties>(jsonToParse)
println("result -> $result")
}
But when I run the main method, I get the following error:
Exception in thread "main" java.lang.IllegalStateException: Only KClass supported as classifier, got T
at kotlinx.serialization.internal.Platform_commonKt.kclass(Platform.common.kt:102)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:52)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
at kotlinx.serialization.SerializersKt__SerializersKt.builtinSerializerOrNull$SerializersKt__SerializersKt(Serializers.kt:79)
at kotlinx.serialization.SerializersKt__SerializersKt.serializerByKTypeImpl$SerializersKt__SerializersKt(Serializers.kt:69)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:54)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
But I am not sure why. Can someone provide me an explanation and if possible some tips on how I can implement this?
It would have worked if you've simply done:
val result = Json.decodeFromString<ContentMessageDto<DummyObjectNonNullProperties>>(jsonToParse)
But with all this wrapping, type information about T was lost. The problem is that you can't simply use reified generics here, cause inline functions can't be non-final.
Possible workarounds:
Define parseContentMessage as extension function so that it could have inline modifier (and T could be reified):
interface MyParser {
val jsonSerialization: Json
}
inline fun<reified T> MyParser.parseContentMessage(json: String): ContentMessageDto<T> {
return jsonSerialization.decodeFromString(json)
}
class MyParserImpl(override val jsonSerialization: Json) : MyParser
//Usage will be the same
Manually pass serializer for T into parseContentMessage:
interface MyParser {
fun <T> parseContentMessage(json: String, contentSerializer: KSerializer<T>): ContentMessageDto<T>
}
class MyParserImpl(private val jsonSerialization: Json) : MyParser {
override fun <T> parseContentMessage(json: String, contentSerializer: KSerializer<T>): ContentMessageDto<T> {
return jsonSerialization.decodeFromString(ContentMessageDto.serializer(contentSerializer), json)
}
}
//Usage:
val result = parser.parseContentMessage(jsonToParse, DummyObjectNonNullProperties.serializer())

How to create an instance of a class by passing the type

I want to be able to say make an instance of this class and give a type then the code can instantiate a new instance of that class.
fun maker(type: Class<Animal>): Animal {
if(type == Class<Dog>) {
return Dog()
}
else if (type == Class<Cat>) {}
...
}
What is a good way to do this?
If they all have zero-argument constructors, you can do:
fun maker(type: Class<Animal>): Animal {
return type.newInstance()
}
You can make it return the type that was passed in for a little more versatility:
fun <T: Animal> maker(type: Class<T>): T {
return type.newInstance()
}
Correct version following your example (not sure if best approach overall):
fun <T: Animal> maker(type: Class<T>): T? {
return when (type) {
Cat::class.java -> Cat() as T
Dog::class.java -> Dog() as T
else -> null
}
}
And then to create objects:
val a = maker(Cat::class.java)
val b = maker(Dog::class.java)
(Updated) I am not an expert in Kotlin but you can do something like this :
import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance
class A {
fun greet() {
println("Hello A");
}
}
class B{
fun greet() {
println("Hello B");
}
}
fun <T : Any> maker(clazz: KClass<T>): T {
return clazz.createInstance();
}
val aObj = maker<A>(A::class);
aObj.greet();
val bObj = maker<B>(B::class);
bObj.greet();
Output:
Hello A
Hello B
I hope now it makes sense you just need to pass the class to the method and it returns an object.
As you will be using Animal as a parent class so you can replace Any => Animal
fun <T : Animal> maker(clazz: KClass<T>): T {
return clazz.createInstance();
}
If the function can be inline you can also use reified type
inline fun<reified T: Animal> make() = T::class.createInstance()
...
val dog = make<Dog>()
Please notice that to use createInstance() the class must have no-arg constructor or IllegalArgumentException will be thrown

Square brackets after function call

In the PagingWithNetworkSample, in the RedditActivity.kt on the line 68 is a function that contains another function call followed by square brackets and the class type (line 78):
private fun getViewModel(): SubRedditViewModel {
return ViewModelProviders.of(this, object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val repoTypeParam = intent.getIntExtra(KEY_REPOSITORY_TYPE, 0)
val repoType = RedditPostRepository.Type.values()[repoTypeParam]
val repo = ServiceLocator.instance(this#RedditActivity)
.getRepository(repoType)
#Suppress("UNCHECKED_CAST")
return SubRedditViewModel(repo) as T
}
})[SubRedditViewModel::class.java]
}
What does this exactly do? Automatically cast to that type? (it's not an array/list to suppose it calls get)
Can you bring an example where this is useful?
That code might look strange, but it's really just a way of calling get(). This would be just as valid, but slightly more verbose:
private fun getViewModel(): SubRedditViewModel {
return ViewModelProviders.of(this, object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
// ...
}
}).get(SubRedditViewModel::class.java)
}

How to return T in Kotlin?

I want to return T in the function.
I have an interface class IRepository.kt
interface IRepository
{
fun <T>Save(model:T)
fun <T>Delete(model:T)
fun <T>Get(id:Long):T
}
I want to implement in Repolmpl.kt
class Repolmpl:IRepository
{
override fun <T>Delete(model:T)
{
println("$model : Save}")
}
override fun <T>Get(id:Long):T
{
return T //ERROR here I want to return T...
}
override fun <T> Save(model: T)
{
println("$model : Delete")
}
}
I saw some similar questions online but I just can't find the right solution.
A generic type T is basically just a template. You cannot return it but have to replace it with an actual type first. Make the interface itself generic, not its methods. When implementing, specify T:
interface IRepository<T> {
fun save(model: T)
fun delete(model: T)
fun get(id: Long): T
}
class Repolmpl: IRepository<String>
{
override fun delete(model: String) {}
override fun get(id: Long): String {}
override fun save(model: String) {}
}
You cannot just return T. T is type here, and it is like return String.
You have to return instance of T. So, sth like:
class Repo {
val data = mapOf<Long, Any>()
// ...
fun <T> get(id: Long): T {
return data[id] as T // Get data from somewhere and then cast it to expected type
}
}

Kotlin type auto boxing vs primitive

class Remember private constructor() {
private var data: ConcurrentMap<String, Any> = ConcurrentHashMap()
private fun <T> saveValue(key: String, value: T): Remember {
data[key] = value
return this
}
private fun <T> getValue(key: String, clazz: Class<T>): T? {
val value = data[key]
var castedObject: T? = null
//Failed here
if (clazz.isInstance(value)) {
castedObject = clazz.cast(value)
}
return castedObject
}
fun putInt(key: String, value: Int): Remember {
return saveValue(key, value)
}
fun getInt(key: String, fallback: Int): Int {
val value = getValue(key, Int::class.java)
return value ?: fallback
}
}
When I putInt(key, 123), 123 is autoboxed to java.lang.Integer. When I get value from the Map, how do I compare value typed Any with Class<T> in which T is Int:class.java in this case? Currently, clazz.isInstance(value) always fails. It works if this class is written in Java
I think that's not kotlin but Java. Map only accepts Object type. So the primitive type will be autoboxed to put in a Map. So value returns from Map is alway Object.