hfhbd
09/07/2022, 4:29 AMprintln("Hello ${Foo.bar}")
but I get println("Hello $Foo.bar")
which is wrong.hfhbd
09/07/2022, 4:30 AMimport com.squareup.kotlinpoet.*
object Foo {
val bar = "World"
}
val member = MemberName(ClassName("", "Foo"), "bar")
val function = FunSpec.builder("main")
function.addStatement("println(\"HELLO $%M\")", member)
function.build().toString()
// expected:
fun main() {
println("HELLO ${Foo.bar}")
}
Do I have to add the brackets by myself? Or do you know some string template function?Paul Woitaschek
09/07/2022, 5:17 AMephemient
09/07/2022, 6:40 AM"""println("HELLO ${%M}")""".replace('$', '$')
to reduce the amount of escaping requiredhfhbd
09/07/2022, 6:40 AMephemient
09/07/2022, 6:40 AMephemient
09/07/2022, 6:41 AMhfhbd
09/07/2022, 6:41 AMephemient
09/07/2022, 6:42 AM·
(which kotlinpoet replaces with space) to ensure that linewrapping doesn't happen in the middle of your stringephemient
09/07/2022, 6:45 AM.addStatement("println(%P)", CodeBlock.of("HELLO \${%M}", member))
works to reduce the amount of quoting inside quotinghfhbd
09/07/2022, 6:49 AMPaul Woitaschek
09/07/2022, 6:50 AM"""Hello ${'$'}"""
iirc 😛hfhbd
09/07/2022, 6:51 AM"""Hello ${"$"}"""
this works too no need for the single quoteshfhbd
09/07/2022, 6:52 AM"""Hello \$"""
is much nicerephemient
09/07/2022, 6:53 AMephemient
09/07/2022, 6:56 AM"""Hello \$""" == "Hello \\\$" == "Hello " + '\\' + '$'
"Hello \$" == "Hello " + '$' == """Hello ${'$'}"""
hfhbd
09/07/2022, 6:56 AM\$
only works with normal quotes, not with triple quotes. What a mess 😄Paul Woitaschek
09/07/2022, 6:59 AM"""hello \$""" == ("hello " + "\\" + "$")
Paul Woitaschek
09/07/2022, 6:59 AMhello \$
Paul Woitaschek
09/07/2022, 6:59 AM