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.
String interpolation in Kotlin has always been straightforward: just prefix your variable with $
, and Kotlin will substitute its value in the string. For example:
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:
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.
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:
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.
Let’s look at a few examples of how multi-dollar string interpolation can simplify different use cases:
Simplifying Templates in Build Scripts:
val artifactId = "my-library"
val template = "$$artifactId-\${version}"
println(template)
// Output: $my-library-${version}
Working with DSLs or External Configuration Files:
val serviceName = "user-service"
val config = "$$$serviceName = $serviceName"
println(config)
// Output: $$user-service = user-service
Handling Complex Placeholders:
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.
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:
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")
Improved Readability:
No more backslashes cluttering your strings. The intent is clear at a glance.
Reduced Errors:
Fewer chances of introducing bugs due to incorrect escaping.
Consistency:
Works seamlessly with existing string interpolation, making the transition easy.
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.
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.
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:
Join our upcoming classes
https://www.androidengineers.in/courses