为什么Scala 中没有字符串插值?

发布于 2024-08-25 19:51:29 字数 152 浏览 11 评论 0原文

这不仅仅是一句闲话……我想知道是否有人知道 Scala 不支持类似于 Groovy 和其他“语法上更好的 Java”的插值是否有实际的设计原因?

例如

var str1 = "World";
var str2 = "Hello, ${str1}";

This is not just an idle quip... I wonder if anybody knows if there's an actual design reason why Scala does not support interpolation similar to Groovy and other "syntactically better Javas"?

e.g.

var str1 = "World";
var str2 = "Hello, ${str1}";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

等待圉鍢 2024-09-01 19:51:29

字符串插值是在 Scala 2.10 中。请参阅 SIP

如果您使用的是早期的 Scala 版本,还有 ScalaEnhancedStrings 编译器插件< /a>.

String interpolation is in Scala 2.10. See the SIP.

If you're using an earlier Scala version, there's also the ScalaEnhancedStrings compiler plugin.

或十年 2024-09-01 19:51:29

建议的解决方案是在使用 + 时省略空格。

"a="+a+", b="+b

它已经建议瑞士键盘布局使输入变得非常自然,因此 Scala 的创建者不会感到足够的痛苦来证明在这个方向上使语言复杂化是合理的:)

The proposed solution is to omit spaces when using +.

"a="+a+", b="+b

It has been suggested that a Swiss keyboard layout makes this very natural to type, so the creators of Scala don't feel enough pain to justify complicating the langauge in this direction :)

深居我梦 2024-09-01 19:51:29

从 Scala 2.10.0 开始,Scala 支持字符串插值

示例:

val name = "James"
println(s"Hello, $name")  // Hello, James

Starting in Scala 2.10.0, Scala does support String Interpolation

Example:

val name = "James"
println(s"Hello, $name")  // Hello, James
陌路黄昏 2024-09-01 19:51:29

人们认为,一方面,由此带来的额外的编译器复杂性不值得获得,另一方面,它的缺失也不会造成太大的负担。

It was deemed that the extra compiler complexity that would result from it was not worth the gains to be had on one hand, and, on the other hand, that its absence would not be overly burdensome.

若有似无的小暗淡 2024-09-01 19:51:29

我感觉字符串插值是不必要的,因为它并不比连接简洁多少:

val str2 = "Hello, ${str1}!"
val str2 = "Hello, "+str1+"!"

争论字符串上的“+”运算符应该格式化而没有空格, Martin Odersky 说

使用新约定,需要进行字符串替换(通常
在 Scala 列表中提出)几乎全部消失。比较:

“测试结果:${result1},${result2}”

与上面的第一个版本。您每保存一个角色
替换,几乎不值得新的语法约定。换句话说,
Scala的字符串替换语法写法

<前><代码>“+...+”

而不是

<前><代码>${...}

或其他类似的提案。另一方面,如果你坚持
+ 周围有空格,故事就会变得不那么令人信服。

顺便说一下,请参阅博客文章“字符串插值在 Scala 中,它是使用反射来模拟的。

I gather the feeling is that String interpolation is unnecessary, because it's not much more concise than concatenation:

val str2 = "Hello, ${str1}!"
val str2 = "Hello, "+str1+"!"

Arguing that the "+" operator on Strings should be formatted without a space, Martin Odersky says,

With the new convention, the need for string substitution (often
put forward on Scala lists) all but disappears. Compare:

"test results: ${result1}, ${result2}"

with the first version above. You have saved one character per
substitution, hardly worth a new syntax convention. In other words,
Scala's string substitution syntax is written

"+...+"

instead of

${...}

or some other similar proposal. On the other hand, if you insist on
spaces around the +, the story becomes much less convincing.

Incidentally, see the blog post, "String Interpolation in Scala" where it's emulated using reflection.

梦幻的心爱 2024-09-01 19:51:29

字符串插值目前正在主干中添加到 Scala,请参阅 Martin 的 此提交,因此它将可能会在 Scala 2.10 中提供。另请参阅第一个测试用例的示例: https:// lampvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/run/stringInterpolation.scala

String interpolation is currently being added to Scala in the trunk, see this commit by Martin, so it will probably be available in Scala 2.10. See also the first test case for an example: https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/run/stringInterpolation.scala

老旧海报 2024-09-01 19:51:29

Scala 对于 XML 确实有类似的功能:

val x = doSomeCrazyCalculation
val xml = <foo>{x}</foo>

Scala does have a similar feature for XML:

val x = doSomeCrazyCalculation
val xml = <foo>{x}</foo>
轻许诺言 2024-09-01 19:51:29

考虑一下 XML 文字仅被计算一次。有关处理多行字符串的问题变体,请参阅 Scala 多行字符串占位符

Consider that the XML literal is evaluated only once. See Scala multiline string placeholder for question variant dealing with multi-line strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文