Kotlin with function

Suneet Agrawal
3 min readJun 5, 2021

Kotlin has made our life very easy by providing features like extension functions, nullability check and much more. One such kind of really helpful feature is Scope functions. Once you understand what scope functions are, you will not able to resist yourself from using them.

This post was originally posted at https://agrawalsuneet.github.io/blogs/kotlin-with-function/ and reposted on Medium on 5th June 2021.

Scope functions are nothing but the functions which define to the scope of the calling object. We can apply operations on that object within that scope and return the object itself from that scope function or we can even return the result of operation or operations from the scope function.

There are a few scope functions

To keep this article short and to the point, we will talk only about with in this article and all the use cases around it.

with is not an extension function to Template class but a normal extension function that takes a Template class object and a higher-order function as parameters, applies the operations to the passed Template class object and return the lambda expression or results. The return type can also be void.
More than functional, with can be used as grammatical where we can read as “with this object, do the following.”.

Why with is not an extension function to Template class?

with is not an extension function to Template class because we don’t want to use it for chaining purpose. For chaining purpose, we have let, also and apply scope functions. with can only be the starting point of any expression. That’s the reason it’s not an extension function to the Template class.

To understand with function lets look at the implementation of with function first.

* Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
return receiver.block()

This clarifies a few things

  1. The return type of the with function is nothing but the last expression we returned from our passed lambda parameter.
  2. Although it's not an extension function to Template class but we can pass any object as the first param to with function.

Now let's understand what is the contract.

The contract is nothing but a contract applied to the passed lambda as a parameter.

* Specifies the contract of a function.
* The contract description must be at the beginning of a function and have at least one effect.
* Only the top-level functions can have a contract for now.
* @param builder the lambda where the contract of a function is described with the help of the [ContractBuilder] members.
public inline fun contract(builder: ContractBuilder.() -> Unit) { }

This is exactly the same contract as to any other scope function. This superimposes some conditions on the lambda we passed as a parameter to the with function. What conditions it superimposed, we need to check the parameter of the contract

And what contract applied in the with function?

Please continue reading at https://agrawalsuneet.github.io/blogs/kotlin-with-function/

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.