Complex lambda function in class constructor

I am working on refactoring java code base to Kotlin. As you can see below code, it include duplicated params declaration in both class constructor and subscribeToPush function. Suppose class take more than one lambda and each include few params, it would be really messy with duplication. Any improvement i could make to avoid this situation

//suppose subscription method 4 params 
class PushNotificationStrategy (val subscription: (context: Context, token: String, ......) -> Observable<CommonChatSubscription>) {

        //Again I have to declare 4 params with types and name here again 
        fun subscribeToPush(context: Context, token: String, .....): Observable<CommonChatSubscription> {
            return subscription(context, token, ......)

        // On and Off strategies 
        companion object {
            val On = PushNotificationStrategy({ context, token ->
                val apiPushSubscription = ApiPushSubscription(context)
                        .onErrorReturn {
                            Timber.e(it, "Push subscription failed with %s", it.message)


            val Off = PushNotificationStrategy({ context, token ->

I’m not sure what exactly you are asking. If you want to make it more obvious that those all take a lambda with the same arguments you can create a typealias for the parameters of the lambda

typealias Subscription = (context: Context, token: String) -> Observable<CommonChatSubscription>
// or
typealias Subscription = (Context, String) -> ... // the names of the arguments are optional

Then your constructor would look like this

class PushNotificationStrategy(val subscription: Subscription) 

This would however not change the calling site. You still need to declare each parameter giving it a name.

Btw you can remove the parentheses around the lambda if it’s the last argument and also use _ for unused parameter names like this:

val Off = PushNotificationStrategy { _, _ -> Observable.just(null) }
1 Like

@Wasabi375, typealias would be what i want for the time being. Thanks