annotate content/Kotlin/multiline-strings.md @ 104:c56fdf7a343d

New blog post about Kotlin's multiline strings
author Dirk Olmes <dirk.olmes@codedo.de>
date Fri, 12 Jun 2020 12:56:06 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
104
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
1 Title: Kotlin multiline strings vs the $ character
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
2 Date: 2020-06-12
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
3 Lang: en
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
4
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
5 I recently started to use [Kotlin](https://kotlinlang.org/) for small projects at work. At first I was very pleased with the language but the more I use it the more I'm stubling over things that are not nice.
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
6
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
7 I'll rumble about the braindead constructor syntax or the epic autoboxing fail with numbers some other time. Today I'll get into [multiline string templates](https://kotlinlang.org/docs/reference/basic-types.html#string-templates) and the use of the `$` character.
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
8
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
9 Kotlin, much like [Groovy](https://groovy-lang.org/) supports multiline strings. It also allows the use of `$`-epxressions in the string. Things start to become difficult if you need a literal dollar sign in the string.
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
10
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
11 Embedding literal dollar signs in Groovy multiline strings is easy:
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
12
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
13 :::java
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
14 name = "Dirk"
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
15 template = """Hello $name,
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
16 I owe you \$100
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
17 but I don't owe you the \$world"""
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
18 println(template)
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
19
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
20 Just escape the dollar sign with `\` and you're good to go.
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
21
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
22 Not so in Kotlin! Using a dollar sign that's escaped with backticks will fail compilation with "unresolved reference". The documentation talks about the "valid solution" for using dollar signs:
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
23
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
24 ::java
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
25 fun main() {
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
26 val name = "Dirk"
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
27 val template = """Hello $name,
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
28 I owe you $100
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
29 but I don't owe you the ${'$'}world"""
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
30 println(template)
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
31 }
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
32
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
33 Note how you don't have to escape the dollar sign in front of the number and the awkward escaping syntax.
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
34
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
35 [I'm not alone with this complaint](https://youtrack.jetbrains.com/issue/KT-2425) as the discussion on the ticket shows. IMHO it's not a good sign that boasts itself for "making developers happier" when little attention to this kind of details is shown. The ticket has been open for 8 years now ...
c56fdf7a343d New blog post about Kotlin's multiline strings
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
36