Hello, I do not understand how to write function ...
# getting-started
a
Hello, I do not understand how to write function with receiver using lambda expression? My example that doesn't work.
Copy code
val userRoutes: Route.() -> Unit = { route: Route -> route.userRoutes(userService) }
But this is working:
Copy code
val tt = { route: Route -> route.userRoutes(userService) }
val userRoutes: Route.() -> Unit = tt
s
For your first example, you can write:
Copy code
val userRoutes: Route.() -> Unit = { this.userRoutes(userService) }
Or just
Copy code
val userRoutes: Route.() -> Unit = { userRoutes(userService) }
The second example works because > Non-literal values of function types with and without a receiver are interchangeable, so the receiver can stand in for the first parameter, and vice versa. > https://kotlinlang.org/docs/lambdas.html#instantiating-a-function-type
a
Sam, thanks) You are very helpful)
🐕 1