https://kotlinlang.org logo
#android
Title
# android
i

ivano

08/13/2019, 12:04 PM
Copy code
@Parcelize
class RemoteDeclarationRepository : DeclarationRepositoryInterface {

    private val declareService: ProActiveDeclareApi
        get() = Api()

    override fun getDeclarations(periodId: Int, offset: Int, limit: Int): Single<DeclarationsResponse> {
        return declareService.getDeclarations(periodId, offset, limit)
    }

    override fun getDeclarationPeriod(id: Int): Single<DeclarationPeriod> {
        return declareService.getPeriod(id).map { it.data }
    }       //and so on 50 times almost
a

Ash

08/13/2019, 12:07 PM
Sorry, what do you mean by `I have to write 50 times the same variable
declareService
inside these override functions`?
i

ivano

08/13/2019, 12:09 PM
That inside this snippet I have almost 50 methods, and inside each of them I have to write
declareService.getthis()
in one function, then the same in the next
declareService.getThat()
I would like to know if in kotlin there is a "wrapper" as the keyword
with
so that I can write only
getThis()
or
getThat
a

arekolek

08/13/2019, 12:57 PM
hmmm, maybe delegation should work?
Copy code
class RemoteDeclarationRepository : DeclarationRepositoryInterface, ProActiveDeclareApi by Api() {

    override fun getDeclarations(periodId: Int, offset: Int, limit: Int): Single<DeclarationsResponse> {
        return getDeclarations(periodId, offset, limit)
    }

    override fun getDeclarationPeriod(id: Int): Single<DeclarationPeriod> {
        return getPeriod(id).map { it.data }
    }
(then
ProActiveDeclareApi
would be a public api of
RemoteDeclarationRepository
, so maybe you don’t want that :P)
i

ivano

08/13/2019, 1:14 PM
COOL
thank you Arek!
2 Views