我可以在 Kotlin 中调用字符串模板内的方法吗?

Al *_*mun 0 kotlin

我有一个方法:

fun sum(first:Int, second:Int):Int
{
    return first + second
}
Run Code Online (Sandbox Code Playgroud)

我可以像使用变量一样在字符串模板内使用参数调用此方法吗?

我尝试了以下方法,但没有成功:

println("$sum(3,4)")
Run Code Online (Sandbox Code Playgroud)

zsm*_*b13 7

是的,字符串模板可以包含任意表达式,您只需使用花括号即可。

fun foo() = 42
val bar = 25

"$bar"
"${bar}"
"${foo()}"
"${2 + 10 / 5}"
Run Code Online (Sandbox Code Playgroud)