# Complex lambda function in class constructor

**URL:** <https://discuss.kotlinlang.org/t/complex-lambda-function-in-class-constructor/8102>\
**Category:** Language Design\
**Created:** [June 10, 2018, 4:34am UTC](https://discuss.kotlinlang.org/t/complex-lambda-function-in-class-constructor/8102 "2018-06-10T04:34:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sannysinghs](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sannysinghs/32/4094_2.png) [@sannysinghs](https://discuss.kotlinlang.org/u/sannysinghs)\
**Post date:** [June 10, 2018, 4:34am UTC](https://discuss.kotlinlang.org/t/complex-lambda-function-in-class-constructor/8102/1 "2018-06-10T04:34:10Z")

</div>

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)
                apiPushSubscription.subscribePushService(token)
                        .onErrorReturn {
                            Timber.e(it, "Push subscription failed with %s", it.message)
                            CommonChatSubscription()
                        }

            })

            val Off = PushNotificationStrategy({ context, token ->
                Observable.just(null)
            })
        }
    }

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 10, 2018, 7:56am UTC](https://discuss.kotlinlang.org/t/complex-lambda-function-in-class-constructor/8102/2 "2018-06-10T07:56:08Z")

</div>

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

```kotlin
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

```kotlin
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](https://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter) if it’s the last argument and also use `_` for unused parameter names like this:

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

```

---

<div class="post-metadata">

**Author:** ![sannysinghs](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sannysinghs/32/4094_2.png) [@sannysinghs](https://discuss.kotlinlang.org/u/sannysinghs)\
**Post date:** [June 14, 2018, 9:21am UTC](https://discuss.kotlinlang.org/t/complex-lambda-function-in-class-constructor/8102/3 "2018-06-14T09:21:58Z")

</div>

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