Kotlin class delegation, passing this to delegate
Asked Answered
I

1

14

Is there any possibility to pass this when delegating class in kotlin?

class SomeFlow : Flow, SmsAuthentication by DefaultSmsAuthentication(this)

It says this does not exist in this context. The other class looks like this:

class DefaultSmsAuthentication(val flow: Flow) : SmsAuthentication
Illustrational answered 23/11, 2017 at 21:10 Comment(2)
Why you need this?Household
I have some legacy code with class SmsAuthenticationFlow. Every flow that want to use sms auth must extend this flow. I would like to delegate it to another class. Authentication uses some of flow dependencies though what makes me realize that I can pass those dependencies only ...Illustrational
A
16

How about injecting this by setter, not by constructor?

For example:

interface SmsAuthentication {

    fun withFlow(flow: Flow)

    fun auth()

}

class DefaultSmsAuthentication() : SmsAuthentication {

    var flow: Flow? = null

    override fun withFlow(flow: Flow) {
        this.flow = flow
    }

    override fun auth() {
        flow?.proceed()
    }

}

class SomeFlow : Flow, SmsAuthentication by DefaultSmsAuthentication() {

    init {
        withFlow(this)
    }

}

However, you need to call withFlow() in constructor by hand every time. You may forget to call it.

You may want to have SmsAuthentication as a property. So you just inject it by lazy and call it in need. I think it's safer way.

class SomeFlow : Flow, SmsAuthentication {

    val auth by lazy { DefaultSmsAuthentication(this) }

    override fun auth() {
        auth.auth()
    }

}

You can also apply Decorator pattern, conversely:

class DefaultSmsAuthenticationFlow(val flow: Flow) :
    SmsAuthentication,
    Flow by flow
{
    override fun auth() {
        // you can use flow as this here
    }
}

fun doAuth(flow: Flow) {
    DefaultSmsAuthenticationFlow(flow).auth()
}
Auberbach answered 24/11, 2017 at 7:16 Comment(1)
also see my question stackoverflow.com/q/76913582/4134215Replacement

© 2022 - 2024 — McMap. All rights reserved.