janvladimirmostert
11/20/2018, 8:58 PMinterface JsonProvider {
fun <T> fromJson(input: String): T
fun <T> fromJsonList(input: String): List<T>
}
Gson for example now requires the class type to be passed in:
class Blah : JsonProvider {
val gson = com.google.gson.Gson()
override fun <T> fromJson(input: String): T {
return gson.fromJson(input, T::class.java)
}
unless that T
is reified and fromJson / fromJsonList are inlined, IntelliJ complains and reifying that T inside the interface doesn't seem to be supported.diesieben07
11/20/2018, 9:30 PMreified
only works for inline
and inline
functions cannot be open
(which version should the compiler inline at the call site?). Since every interface method is open
, interface methods cannot reasonably use reified
.
You should accept a Class<T>
in the interface method and then use an inline
extension function which has a reified
parameter and delegates to the actual interface method.Ruckus
11/21/2018, 5:40 AMinterface JsonProvider {
fun <T> fromJson(input: String, type: Class<T>)
}
inline fun <reified T> JsonProvider.fromJson(input: String) = fromJson(input, T::class.java)
janvladimirmostert
11/21/2018, 7:39 AM