对于您的用例,它们可以很容易地实现很好的语法。
scala> implicit class `string quoter`(val sc: StringContext) {
| def q(args: Any*): String = "\"" + sc.s(args: _*) + "\""
| }
defined class string$u0020quoter
scala> q"hello,${" "*8}world"
res0: String = "hello, world"
scala> "hello, world"
res1: String = hello, world // REPL doesn't add the quotes, sanity check
scala> " hello, world "
res2: String = " hello, world " // unless the string is untrimmed
松鼠隐藏在包对象的某处。
当然,除了q
之外,您可以命名插补器。
上周,有人要求ML能够使用反引号标识符。现在,你可以做RES3但不RES4:
scala> val `"` = "\""
": String = "
scala> s"${`"`}"
res3: String = "
scala> s"hello, so-called $`"`world$`"`"
res4: String = hello, so-called "world"
刚刚发生,我是,F-插值已经做了一些工作,以按摩的字符串另一个想法。例如,它必须智能地处理“%n”。它可以在同一时间处理额外的转义“%q”,它不会传递给底层的格式化程序。
这将是这样的:
scala> f"%qhello, world%q"
<console>:9: error: conversions must follow a splice; use %% for literal %, %n for newline
这是值得的增强请求。
更新:刚才注意到,八进制未在插值弃用尚未:
scala> s"\42hello, world\42"
res12: String = "hello, world"
当你使用三引号,你不需要反斜线 – sschaef