0

In Java, now that it supports text blocks, you can do this:

@Schema(description = """
            Line one.
            Line two.
            """)
public void someMethodName() { ... }

In Java, text blocks are compile-time constants and they automatically remove the indents. But in Kotlin, if you do this:

@Schema(description = """
            Line one.
            Line two.
            """)
fun someMethodName() { ... }

you end up with unwanted spaces in front of each line. Unfortunately, you can't use trimMargin() or trimIndent() because they are not compile-time constants. Is there a way to make it look as nice in Kotlin as it does in Java?

k314159
  • 5,051
  • 10
  • 32

1 Answers1

0

Unfortunately for your use case, I don't think so. The point of the triple quote is to provide a way to write "Formatted" text into a string. If Java doesn't behave the same way as Kotlin, then technically it's the odd one out as any other language I've used behaves the same way as Kotlin. Your best alternative would be something like the following:

@Schema(description = "Line one.\n"
                    + "Line two.\n"
                    + "Line three.")
fun someMethodName() { ... }

The string concatenation will be performed at compile time since it is between literals.

flamewave000
  • 547
  • 5
  • 12
  • 1
    "If Java doesn't behave the same way as Kotlin, then technically it's the odd one out" C# text blocks also treat leading spaces in the same way as Java, which I think is **far** more useful. If you have a text block where all lines are indented with >=3 tabs, then three tabs will be trimmed from the beginning of each line, keeping any extra leading whitespace after those three tabs. This is a feature that most languages **should** have (but don't), given how common multiline strings are, and it's embarassing to see that Java is actually ahead of Kotlin with this... – Nyde Dec 13 '22 at 16:42