I will be using observable transformer in my application for converting data from one type to another, so for this use i created a generic class with one parameter <T1>
where T1 is the type am expecting to get the output
Example
class Transformer<T1> {
fun transform(): ObservableTransformer<String, T1> {
return ObservableTransformer { data ->
data.map {
if (T1 is Int) { //getting compilation error like T1 is not an expression
return IntDataType
}
if (T1 is Double) { //getting compilation error like T1 is not an expression
return DoubleDataType
}
}
}
}
}
The above class is the transformer which gets the input as string and convert String to someData type based on given generic type T1
So i will be calling the above like this
getSomeData()
.compose(Transformer<Double>().tranform())
.map{ it: double
}
is there is any other solution to perform like this type of transformer ? Any help
The T1 is Int
non-statement doesn't work, because T1
is a generic type and while is Int
checks the type of an object: 1 is Int
You could write a dedicated transformer instead of using generics for it. In general you're loosing the type information of T1
on runtime.
fun Observable<String>.asDouble() = map { it.toDouble() }
getSomeData().asDouble().map { value: Double -> }
You don't even need an actual transformer as it's a simple mapping.