Group Group Group Group Group Group Group Group Group

Chapter 3 Code Sample


#1

Looks like this code snippet is Swift instead of Kotlin :grin:

let bigString = """
  |You can have a string
  |that contains multiple
  |lines
  |by
  |doing this.
  """.trimMargin()
println(bigString)

To fix, change it to:

val bigString = """
  |You can have a string
  |that contains multiple
  |lines
  |by
  |doing this.
  """.trimMargin()
println(bigString)

#2

In the explanation for the above code, they mention the pipe (|) and the function trimMargin(), but the explanation doesn’t quite jive with the actual code. i.e., indentation can be achieved without using the pipe at all, so it’s not at clear what effect, if any, the pipe is achieving.


#3

Great catch! Thank you so much for reporting this, we really appreciate it.

I have submitted the let to val fix to our editor, as well as an updated explanation copied below. Please let me know what you think!

The three double-quotes signify that this is a multi-line string. Handily, trimMargin() will remove new lines at the beginning and end of the string to remove unnecessary white space. This way, you are not forced to put the """ on the same line as the string. Finally, the | character (known as a “pipe”) ensures that all preceding white space from each line is removed, giving you more flexibility in indentation without affecting the output.


#4

sounds great, thanks!