https://kotlinlang.org logo
#announcements
Title
# announcements
a

Alex Spence

06/24/2019, 7:52 PM
The JSON parsing should be parsing the object into an Int, but somewhere I need to cast it from any
c

cbruegg

06/24/2019, 7:56 PM
Maybe this little trick could help you (untested):
Copy code
inline fun <reified T> MetricFilter(filterId: String, val defaultValue: T?) = MetricFilter(filterId, defaultValue, { it as T })

data class MetricFilter<T>(val filterId: String, val defaultValue: T?, private val caster: (Any?) -> T) {
    fun foo(...): ... {
        val value: Any? = ...
        return MetricFilterValue(this, caster(value))
    }
}
Basically, you provide the
MetricFilter
with a function that casts the value
a

Alex Spence

06/24/2019, 8:04 PM
Hmm still getting cannot use T as reified type parameter, use class instead
when I try to call metricFilter
in my builder
Seemed to work around that.
3 Views