https://kotlinlang.org logo
Title
k

karelpeeters

08/15/2018, 8:02 AM
Because a single converter instance can convert multiple different types.
a

aleksandrsl

08/15/2018, 8:55 AM
Can't we just use implementation parametrized with
Any
in this case (e.g.
DefaultConverter
), and we will have types in other cases?
import com.beust.klaxon.JsonValue

interface Test<T> {
    
    fun fromJson(jv: JsonValue): T
    
    fun toJson(value: T): JsonValue
}

class TestImpl: Test<Any> {
    override fun fromJson(jv: JsonValue): Any {
        TODO("not implemented")
    }

    override fun toJson(value: Any): JsonValue {
        TODO("not implemented") 
    }
}
k

karelpeeters

08/15/2018, 8:57 AM
But what about the
canConvert
function then?
And if you add that you might as well remove the illusion of type safety entirely.
a

aleksandrsl

08/15/2018, 9:19 AM
override fun canConvert(cls: Class<*>) = true
seems absurd too. With generic interface you can create custom converter that returns object of concrete class, I see type safety here
k

karelpeeters

08/15/2018, 9:24 AM
Is that how you implement that method? Its supposed to be
... = cls == MyType::class
or something like that.
Or for multiple types
= cls == MyType::class.java || cls == MyOtherType::class.java
.
a

aleksandrsl

08/15/2018, 9:38 AM
k

karelpeeters

08/15/2018, 9:42 AM
Well that's a special case since it's the fallback one, but it's indeed strange that it returns
true
.
For custom converter implementations you usually don't do that
a

aleksandrsl

08/15/2018, 10:50 AM
Thanks for clarification) however it seems somewhat strange that you're writing converter that creates objects of single class and after convertion you must to cast this object. Need time to reconcile with this)
k

karelpeeters

08/15/2018, 10:57 AM
If you want you can write a wrapper abstract class that takes in the correct class and then places more restrictions on the functions.
c

cedric

08/15/2018, 11:33 AM
FYI, I went back and forth between making the converter generic or not, and it was generic in the previous version of Klaxon. However, I decided that the current implementation is more flexible
👍 2