Using underscore ("_") immediately after a variable in string templates in Kotlin

In Kotlin, I am trying to create a file name dynamically, including type and date, for example:

var filename = "ab_$type_$date.dat"

However, the second underscore between the variables causes a compilation error:

Kotlin: Unauthorized link: name _

I know that I could concatenate strings in the old way:

var filename = "ab_" + type + "_$date.dat"

But I wonder if there is another way to do the same. Is there a way to avoid special characters in string patterns or any other way to make this work?

+4
source share
1 answer

Just wrap the expressions in braces:

var filename = "ab_${type}_${date}.dat"
+9
source

All Articles