Kotlin templates are evaluated at compile time - so this won't work.
You should use a 3rd party template engine.
Freemarker is such an engine with a format very similar to Kotlin's own templating format:
val tm = "x = \${x}"
fun fn (x: String) : String {
val t = Template("name", StringReader(tm), Configuration(Configuration.VERSION_2_3_26))
val out = StringWriter()
t.process(mapOf("x" to x) ,out)
return out.toString()
}
println (fn("This is X!!!")) // x = This is X!!!
Two notes:
- You won't be able to use
"$x" on freeMarker, only "${x}"
$ sign can be escaped in a Kotlin string using \$