- Learning Scala Programming
- Vikash Sharma
- 174字
- 2021-06-30 19:07:53
The s interpolator
First, we'll look at the s interpolator. We've already seen how we can create a processed String with variables. Now, let's take an example that takes on expressions:
scala> val nextYearAge = s"Next Year, I'll complete ${age + 1}."
nextYearAge: String = Next Year, I'll complete 26.
Here, we used ${...} where a $ sign is followed by a pair of braces {<expression>}, consisting of the expression to be evaluated. It can be any expression. An arithmetic operation like we just did, or a method call:
scala> def incrementBy1(x: Int) = x + 1
incrementBy1: (x: Int)Int
scala> val nextYearAge = s"Next Year, I'll complete ${incrementBy1(age)}."
nextYearAge: String = Next Year, I'll complete 26.
Here, we defined a method named incrementBy1 that increments any Int passed by 1 and gives back the result. We've called that method from our interpolator. By the way, it's good to know that our interpolator s is a method just like any other operator in Scala. We're allowed to create our own interpolators in Scala.
推薦閱讀
- SQL Server 2016從入門到精通(視頻教學(xué)超值版)
- Oracle Database In-Memory(架構(gòu)與實踐)
- Unity 2020 Mobile Game Development
- 微服務(wù)設(shè)計原理與架構(gòu)
- Git高手之路
- Mastering LibGDX Game Development
- Learning Three.js:The JavaScript 3D Library for WebGL
- 微服務(wù)從小白到專家:Spring Cloud和Kubernetes實戰(zhàn)
- Principles of Strategic Data Science
- 智能手機(jī)故障檢測與維修從入門到精通
- Java 9 Programming By Example
- Mapping with ArcGIS Pro
- Spring Boot從入門到實戰(zhàn)
- C#網(wǎng)絡(luò)編程高級篇之網(wǎng)頁游戲輔助程序設(shè)計
- Swift iOS Programming for Kids