Kotlin Programming Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

We have already seen how we can use string templates with expressions in print statements. Remember how we were able to access a variable using the $ symbol before the name of a variable:

println("$x is a magic number”)

We can also put a piece of code in a string, which is then evaluated and whose results are concatenated into the string. In this case, $ is followed by {}, inside which we put our code:

println("lies between 1 to 10, value: ${if(x.value < x.max) x.value else x.max}")