Kotlin's New Multi-Dollar Feature
Kotlin
Kotlin's New Multi-Dollar Feature
Akshay Nandwana
January 5, 2025
5 min read
125 views

String interpolation is one of Kotlin's most loved features, making code cleaner and more readable. With Kotlin 2.1, the language introduces multi-dollar string interpolation, a small but impactful feature that addresses a common pain point for developers who work with templated strings or configuration files containing $ symbols.

In this blog, we'll explore what this feature is, why it was introduced, and how it simplifies your code.

Why Multi-Dollar String Interpolation?

String interpolation in Kotlin has always been straightforward: just prefix your variable with $, and Kotlin will substitute its value in the string. For example:

kotlin
val name = "Kotlin"
println("Welcome to $name!")
// Output: Welcome to Kotlin!

However, issues arise when you need to work with text containing literal $ symbols, such as configuration templates or scripting languages. Previously, developers had to escape the $ symbol using a backslash (\$), which made the code less readable and more error-prone. Consider this example:

kotlin
val version = "1.0.0"
val template = "\${project.version}: $version"
println(template)
// Output: ${project.version}: 1.0.0

While functional, this approach is cumbersome. Enter multi-dollar string interpolation, which provides a more intuitive way to handle such scenarios.

What is Multi-Dollar String Interpolation?

The new multi-dollar string interpolation feature allows you to use additional $ symbols to include literal $ characters in your strings without escaping them. This makes your code cleaner and easier to read.

Here’s how it works:

  • A single $ still represents a variable to be interpolated (just like before).

  • $$ outputs a single literal $.

  • Additional $ symbols ($$$, $$$$, etc.) allow even more embedded $ symbols when needed.

Example:

kotlin
val version = "1.0.0"
val template = "$$project.version: $version"
println(template)
// Output: ${project.version}: 1.0.0

This syntax removes the need for escaping and makes it much clearer when you intend to include literal $ symbols.

How Does It Work in Practice?

Let’s look at a few examples of how multi-dollar string interpolation can simplify different use cases:

  1. Simplifying Templates in Build Scripts:

kotlin
val artifactId = "my-library"
val template = "$$artifactId-\${version}"
println(template)
// Output: $my-library-${version}
  1. Working with DSLs or External Configuration Files:

kotlin
val serviceName = "user-service"
val config = "$$$serviceName = $serviceName"
println(config)
// Output: $$user-service = user-service
  1. Handling Complex Placeholders:

kotlin
val env = "production"
val template = "$$${env}_CONFIG_PATH"
println(template)
// Output: $$production_CONFIG_PATH

These examples showcase how multi-dollar string interpolation makes it easier to manage strings with complex placeholders, reducing the need for manual escaping.

  1. Android Example — Logging Configuration:

In Android, you might generate log tags dynamically or work with configuration strings for debugging tools. Multi-dollar interpolation simplifies this:

kotlin
val logTag = "MyApp"
val logMessage = "$$$TAG: $logTag"
println(logMessage)
// Output: $$TAG: MyApp

val configPath = "$$${BuildConfig.FLAVOR}_CONFIG"
println(configPath)
// Output: $$demo_CONFIG (if BuildConfig.FLAVOR = "demo")

Key Benefits of Multi-Dollar String Interpolation

  1. Improved Readability:

    • No more backslashes cluttering your strings. The intent is clear at a glance.

  2. Reduced Errors:

    • Fewer chances of introducing bugs due to incorrect escaping.

  3. Consistency:

    • Works seamlessly with existing string interpolation, making the transition easy.

When Should You Use Multi-Dollar String Interpolation?

This feature is particularly useful in scenarios like:

  • Configuration Management: Writing templates for build systems (Gradle, Maven, etc.) or configuration files.

  • Dynamic String Templates: Creating strings with placeholders for external systems.

  • Scripting: Embedding code snippets or DSLs that rely heavily on $ symbols.

If you frequently encounter $ in your strings, this feature can save you time and effort.

Getting Started

To use multi-dollar string interpolation, make sure you’re using Kotlin 2.1 or later. If you haven’t upgraded yet, now is a great time to do so! You can find detailed instructions for upgrading in the official Kotlin documentation.

Conclusion

Kotlin's multi-dollar string interpolation is a perfect example of how small language improvements can make a big difference in developer experience. By addressing a common pain point, this feature enhances readability and maintainability in projects that deal with templated strings.

Akshay Nandwana
Founder AndroidEngineers

You can connect with me on:


Book 1:1 Session here
Click Here

Join our upcoming classes
https://www.androidengineers.in/courses

Share This Article
Stay Updated

Get the latest Android development articles delivered to your inbox.