ayan
10/01/2020, 3:28 AMdoSomething()
(inside a class) that does some work. This doSomething()
works with data of RealDatatypeA
that implements the builder pattern. This doSomething()
is called from a different randomFunction()
that passes into it the data of datatypeA
. I have a similar randomFunction2()
that works with datatypeB
which also implements the builder. In effect, both functions do identical tasks but work with different data types and have different call sites. I want to consolidate the 2 operations so that instead of having 2 copies of similar looking functions, i can have just one.
To do this, I made an interface interface TheThing<T>
.
interface TheThing<T> {
fun setSomeProperty(id): T
}
This interface is implemented as shown below and the implementation needs to mirror all the builder functions to the original data type (RealDatatypeA
) as shown below
class Impl: TheThing<RealDatatypeA.Builder> {
override fun setSomeProperty(id: String): RealDatatypeA.Builder {
return pp.setSomeProperty(id)
}
}
I use this interface in doSomething()
as a param
class SomeClass @Inject constructor(
private val notImportant: NotImportant
){
fun <P:TheThing<P>> doSomething(dataA: P)
...
Now I need to call doSomething()
from another function someClass.doSomething()
like this:
fun randomFunction() {
someClass.doSomething(datatypeA)
This datatypeA
can also be datatypeB
(depending on when this gets called). So here are the 2 questions:
1. In the Impl
, how do I actually pass on the interface builder calls to the real builder calls?
2. In randomFunction()
, I can’t call doSomething(datatypeA)
just like this. I see an error that says Required: TheThing<TypeVariable(P)> Found: RealDatatypeA
How do I solve for this?