Skip to content

Commit 840c425

Browse files
authored
update: update the multi-dollar string interpolation docs (#4924)
* update: update the multi-dollar string interpolation docs * fix: review
1 parent 84ccb59 commit 840c425

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

docs/topics/coding-conventions.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,12 +865,28 @@ Omit semicolons whenever possible.
865865

866866
### String templates
867867

868-
Don't use curly braces when inserting a simple variable into a string template. Use curly braces only for longer expressions.
868+
Don't use curly braces when inserting a simple variable into a string template. Use curly braces only for longer expressions:
869869

870870
```kotlin
871871
println("$name has ${children.size} children")
872872
```
873873

874+
Use [multi-dollar string interpolation](strings.md#multi-dollar-string-interpolation)
875+
to treat the dollar sign chars `$` as string literals:
876+
877+
```kotlin
878+
val KClass<*>.jsonSchema : String
879+
get() = $$"""
880+
{
881+
"$schema": "https://json-schema.org/draft/2020-12/schema",
882+
"$id": "https://example.com/product.schema.json",
883+
"$dynamicAnchor": "meta",
884+
"title": "$${simpleName ?: qualifiedName ?: "unknown"}",
885+
"type": "object"
886+
}
887+
"""
888+
```
889+
874890
## Idiomatic use of language features
875891

876892
### Immutability

docs/topics/strings.md

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,6 @@ ${'$'}_9.99
161161

162162
### Multi-dollar string interpolation
163163

164-
> The multi-dollar string interpolation is [Experimental](components-stability.md#stability-levels-explained)
165-
> and opt-in is required (see details below).
166-
>
167-
> It may be changed at any time. We would appreciate your feedback in [YouTrack](https://youtrack.jetbrains.com/issue/KT-2425).
168-
>
169-
{style="warning"}
170-
171164
Multi-dollar string interpolation allows you to specify how many consecutive dollar signs are required to trigger interpolation.
172165
Interpolation is the process of embedding variables or expressions directly into a string.
173166

@@ -186,7 +179,7 @@ val KClass<*>.jsonSchema : String
186179
{
187180
"$schema": "https://json-schema.org/draft/2020-12/schema",
188181
"$id": "https://example.com/product.schema.json",
189-
"$dynamicAnchor": "meta"
182+
"$dynamicAnchor": "meta",
190183
"title": "$${simpleName ?: qualifiedName ?: "unknown"}",
191184
"type": "object"
192185
}
@@ -222,24 +215,7 @@ println(requestedData)
222215

223216
Here, the `$$$` prefix allows the string to include `$` and `$$` without requiring the `${'$'}` construct for escaping.
224217

225-
To enable the feature, use the following compiler option in the command line:
226-
227-
```bash
228-
kotlinc -Xmulti-dollar-interpolation main.kt
229-
```
230-
231-
Alternatively, update the `compilerOptions {}` block of your Gradle build file:
232-
233-
```kotlin
234-
// build.gradle.kts
235-
kotlin {
236-
compilerOptions {
237-
freeCompilerArgs.add("-Xmulti-dollar-interpolation")
238-
}
239-
}
240-
```
241-
242-
This feature doesn't affect existing code that uses single-dollar string interpolation.
218+
Multi-dollar string interpolation doesn't affect existing code that uses single-dollar string interpolation.
243219
You can continue using a single `$`
244220
as before and apply multi-dollar signs when you need to handle literal dollar signs in strings.
245221

0 commit comments

Comments
 (0)