DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • How to Protect Your AI Agents from Prompt Injection Attacks: An Active Defense Approach
  • Calling GCP From AWS Without Static Keys Using Open-Source MultiCloudJ
  • Building a Voice-Controlled Graph Assistant With Neo4j, LiveKit, and OpenAI
  • What Is Agentic Test Creation and How Is It Different from AI Test Generation?

Toying With Kotlin’s Context Receivers

Kotlin added the idea of Context Receivers in version 1.6.20. In this post, I'd like to toy with them to understand how useful they can be.

By 
Nicolas Fränkel user avatar
Nicolas Fränkel
·
Updated May. 19, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
4.0K Views

Join the DZone community and get the full member experience.

Join For Free

Kotlin added the idea of Context Receivers in version 1.6.20. In this post, I'd like to toy with them to understand how useful they can be.

Note that if you want to play along, you'll need to compile with the -Xcontext-receivers flag.

The main idea behind context receivers is to pass additional parameters to a function without having to do it explicitly.

A Simplified Model Sample

Let's start with a simple example to show how it works. We want to model a simple transfer operation between two bank accounts. Accounts balance is stored in a database, and respective credit/debut operations must be transactional.

Model of simple transfer operation between two bank accounts

Let's focus on the AccountService.transfer() function. It requires a Transaction instance that wraps several operations:

Kotlin
 
class AccountService {
    fun transfer(tx: Transaction, vararg operations: () -> Unit) {
        tx.start()
        try {
            operations.forEach { it.invoke() }
            tx.commit()
        } catch (e: Exception) {
            tx.rollback()
        }
    }
}


We can call the above code as:

Kotlin
 
val service = AccountService()
val transaction = Transaction()
val repo = AccountRepo()
service.transfer(
    transaction,
    { repo.credit(account1, 10.5) },
    { repo.debit(account2, 10.5) }
)


Improving the Code With Extension Functions

We can slightly improve the above code by making use of extension functions. Instead of defining the Transaction as a parameter to the transfer() function, we can migrate the latter to an extension function.

Kotlin
 
class AccountService {
    fun Transaction.transfer(vararg operations: () -> Unit) {
        start()                                                    // 1
        try {
            operations.forEach { it.invoke() }
            commit()                                               // 1
        } catch (e: Exception) {
            rollback()                                             // 1
        }
    }
}


1.  Implicit this References the Transaction Object

Within the context of an AccountService, we can now call the transfer() function on an existing Transaction.

Kotlin
 
with(service) {                               // 1
    transaction.transfer(                     // 2
        { repo.credit(account1, 10.5) },
        { repo.debit(account2, 10.5) }
    )
}


2.  Bring the service instance in scope so it's valid to call transfer on the transaction object.

One can analyze the new calling code from two different viewpoints:

  • The raw number of characters typed is less: it's more concise.
  • The semantics are radically different, though. The new code means that in the context of an AccountService, we can call transfer() on an existing Transaction object.

I think the semantics are wrong: it should be the opposite. In the context of Transaction, we should be able to call transfer() on an existing AccountService object:

Kotlin
 
with(transaction) {
    service.transfer(
        { repo.credit(account1, 10.5) },
        { repo.debit(account2, 10.5) }
    )
}


IMHO, conciseness has very little value compared to the cost of wrong semantics. Unfortunately, with the current language constructs, fixing semantics means we would need to move the transfer() function to Transaction. It would be lousy modeling as the transfer is the responsibility of the service.

Context Receivers to the Rescue

As I mentioned in the introduction, the idea behind context receivers is to somehow "pass" function parameters without being explicit about them.

Kotlin
 
context(Foo, Bar, Baz)
fun myfunction() {}


To call such a function, one needs to bring an object of each contextual type "in scope." We can achieve it with the with function:

Kotlin
 
val foo = Foo()
val bar = Bar()
val baz = Baz()
with(foo) {                     // 1
    with(bar) {                 // 2
        with (baz) {            // 3
            myfunction()        // 4
        }
    }
}


  1. Bring foo in scope.
  2. Bring bar in scope.
  3. Bring baz in scope.
  4. Call the function.

While the code above compiles, it's only applicable if we use the contextual objects. The calling syntax is the same as the one of lambdas with receiver:

Kotlin
 
context(Foo, Bar, Baz)
fun myfunction() {
    println(this@Foo)
    println(this@Bar)
    println(this@Baz)
}


We use context receivers to be able to write code using the wanted code:

Kotlin
 
class AccountService {
    context(Transaction)
    fun transfer(vararg operations: () -> Unit) {
        start()                                   // 1
        try {
            operations.forEach { it.invoke() }
            commit()                              // 1
        } catch (e: Exception) {
            rollback()                            // 1
        }
    }
}


  1. Implicit this references the Transaction object. We don't need to qualify further with the class name as there's no other context object.

We can now call the code accordingly, with the correct semantics:

Kotlin
 
with(transaction) {                               // 1
    service.transfer(                             // 2
        { repo.credit(account1, 10.5) },
        { repo.debit(account2, 10.5) }
    )
}


  1. Bring transaction in scope.
  2. Use the transaction object in scope.

Discussion

Context receivers allow us to implement the API with the correct calling code semantics. It's not possible without them.

I've dabbled only a bit in Scala, but I've always found Scala 2's implicit poorly implemented. To bring an object in scope, you only need an import at the top of the file, which might be very far from where it's used. It makes understanding the code much harder and increases maintenance costs. Scala 3 has made the implicitness much more explicit and fixed some of my grievances.

I believe that Kotlin's implementation is much saner. You achieve scoping in context receivers with with, which brings the context close to the call site and signals it with a code block.

However, it's not all unicorns and rainbows. In particular, I'm a bit worried that context receivers will be abused. Granted, it's generally the case for every new feature for every language. Yet, I feel the potential for abuse is enormous with this one. Only the future will tell.

In the meantime, I'm curious to see more different usages of context receivers and what patterns they can unlock.

To go further:

  • "KEEP: Context receivers"
  • Context Receivers Are Coming to Kotlin!
  • When to use context receiver?
Receiver (information theory)

Published at DZone with permission of Nicolas Fränkel. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook