Kotlin 是有多喜欢美元?

2,492 阅读1分钟

1.jpg

我们都知道,Kotlin 支持 String 插值,即使用美元符号 —— $ 在字符串模板中对变量或者表达式求值:

fun main() {
    val price = 3.4f
    println("Latte price is $price")
}

// Ouput
// Latte price is 3.4

但是,如果我真的需要在一个单词前面使用美元符号呢?写起来稍微有点复杂:

fun main() {
    val lattePrice = 3.4f
    val cappuccinoPrice = 5.7f
    val espressoPrice = 6.8f

    val priceBoard = """
        ${"$"}latte       $lattePrice
        ${"$"}cappuccino  $cappuccinoPrice
        ${"$"}espresso    $espressoPrice
    """.trimIndent()

    println(priceBoard)
}

// Output
// $latte       3.4
// $cappuccino  5.7
// $espresso    6.8

我们必须在单词前面使用 ${"$"} 或者 ${'$'} 来插入一个美元符号,太麻烦了,对吧。

好在,从 Kotlin 2.1.0 开始,引入了对多美元符号字符串插值的支持,改进了在字符串字面量中处理美元符号 $ 的方式。这对模板引擎、JSON 模式或其他数据格式等需要多个美元符号的场景中非常有用。

因为该特性在 2.1.0 版本还是 preview 阶段,所以想启用该功能,需要进行如下设置。

确保使用 K2 模式:

2.png

添加编译选项:

kotlin {
    compilerOptions {
        freeCompilerArgs.add("-Xmulti-dollar-interpolation")
    }
}

完成以上步骤之后,我们便可以和 $ 愉快的玩耍啦:

fun main() {
    val lattePrice = 3.4f
    val cappuccinoPrice = 5.7f
    val espressoPrice = 6.8f

    val priceBoard = $$"""
        $latte       $$lattePrice
        $cappuccino  $$cappuccinoPrice
        $espresso    $$espressoPrice
    """.trimIndent()

    println(priceBoard)
}

// Output
// $latte       3.4
// $cappuccino  5.7
// $espresso    6.8

眼尖的读者可能会发现,怎么插值的时候,也变成了两个美元符号—— $$ 了?。

没错,该特性其实是 配置触发插值所需的美元符号数量,较少的美元符号将被视为字符串字面量。

意思就是说,如果我们前面配置的是 $$,那么只有使用 $$ 的情况下,才会进行插值,比这个少的情况下,将会视为普通字符串。

我们验证一下,假设我们丧心病狂的使用 $$$,那么插值也需要使用 $$$

fun main() {
    val lattePrice = 3.4f
    val cappuccinoPrice = 5.7f
    val espressoPrice = 6.8f

    val priceBoard = $$$"""
        $latte         $$$lattePrice
        $cappuccino    $$$cappuccinoPrice
        $espresso      $$$espressoPrice
    """.trimIndent()

    println(priceBoard)
}

走火入魔使用 $$$$$

fun main() {
    val lattePrice = 3.4f
    val cappuccinoPrice = 5.7f
    val espressoPrice = 6.8f

    val priceBoard = $$$$$"""
        $$$latte         $$$$$lattePrice
        $$$cappuccino    $$$$$cappuccinoPrice
        $$$espresso      $$$$$espressoPrice
    """.trimIndent()

    println(priceBoard)
}

// Output
// $$$latte         3.4
// $$$cappuccino    5.7
// $$$espresso      6.8

正如代码表现的那样,较少的美元符号将被视为字符串字面量。