xref: /aosp_15_r20/external/kotlinpoet/docs/l-for-literals.md (revision 3c321d951dd070fb96f8ba59e952ffc3131379a0)
1%L for Literals
2===============
3
4Although Kotlin's string templates usually work well in cases when you want to include literals into
5generated code, KotlinPoet offers additional syntax inspired-by but incompatible-with
6[`String.format()`][formatter]. It accepts **`%L`** to emit a **literal** value in the output. This
7works just like `Formatter`'s `%s`:
8
9```kotlin
10private fun computeRange(name: String, from: Int, to: Int, op: String): FunSpec {
11  return FunSpec.builder(name)
12    .returns(Int::class)
13    .addStatement("var result = 0")
14    .beginControlFlow("for (i in %L..<%L)", from, to)
15    .addStatement("result = result %L i", op)
16    .endControlFlow()
17    .addStatement("return result")
18    .build()
19}
20```
21
22Literals are emitted directly to the output code with no escaping. Arguments for literals may be
23strings, primitives, and a few KotlinPoet types described below.
24
25 [formatter]: https://developer.android.com/reference/java/util/Formatter.html
26