Hey all, I have a question about precompiled strings that I'd like to be able to use in annotations ...
m
Hey all, I have a question about precompiled strings that I'd like to be able to use in annotations (so they must be
const val
and cannot be
val
). Does anybody have any suggestions? The test below fails right now, but I'd like to make it pass by only adding code. I'd love to use
.trimIndent()
when declaring
myString
but I can't. Is there something I can use that the compiler will unfold?
Copy code
const val myString = """
    This is my string. I wrap it across multiple lines when it
    becomes too long, but there should be no lines visible when
    using this string. In other words, the newlines should be
    compiled out of this string, and the margin collapsed. Is
    there any compiler plugin I can use that can help make this
    a reality?
"""

class ExampleTest {
    @Test
    fun `precompiled multi-line string has no newlines`() {
        expectThat(myString).doesNotContain('/n')
    }
}
v
I doubt it, but I'm no Kotlin expert. But any method call would make it not a constant anymore. Besides taht
trimIndent
would trim the indents, but not kill the linebreaks. Can't you just make the multi-line string a string concatenation, iirc that is something the compiler can handle:
Copy code
const val myString =
    "This is my string. I wrap it across multiple lines when it " +
    "becomes too long, but there should be no lines visible when " +
    "using this string. In other words, the newlines should be " +
    "compiled out of this string, and the margin collapsed. Is " +
    "there any compiler plugin I can use that can help make this " +
    "a reality?"
👆 2
k
By the way, you've got a typo -
'/n'
should be
'\n'
âž• 1