Scala多行字符串占位符 [英] Scala multiline string placeholder

查看:407
本文介绍了Scala多行字符串占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与(为什么在其中没有字符串插值有关Scala?),但更具体地讲多行字符串.

This question is related to ( Why is there no string interpolation in Scala? ), but deals more specifically with multi-line strings.

我刚刚接受了Martin关于简单字符串占位符的建议,其中 msg =你好{name}!"

I've just about bought into Martin's suggestion for simple string placeholder where msg = "Hello {name}!"

可以这样表示: msg =你好" +名称+!"

can be be represented without much difference in Scala today like this: msg = "Hello"+name+"!"

但是,我认为这种方法不适用于多行字符串.而且,在某些情况下,它可能会鼓励其他不良做法,以提高可读性.请注意,在Scala Play ANORM数据库映射中,框架如何尝试保持纯SQL(使用占位符)的可读性,但以复制{countryCode}变量名和以非类型安全的方式进行复制为代价,请参阅... .on("countryCode"->"FRA")

However, I don't think that approach holds with multi-line strings. And, in some cases it may be encouraging other poor practices in favor of readability. Note that in the Scala Play ANORM database mapping how the framework tries to preserve readability in plain SQL (using placeholders), but at the expense of duplicating the {countryCode} variable name and in a non-type-safe way, see... .on("countryCode" -> "FRA")

SQL(
    """
        select * from Country c 
        join CountryLanguage l on l.CountryCode = c.Code 
        where c.code = {countryCode};
    """
 ).on("countryCode" -> "FRA")

此外,假设Scala没有进行任何更改以解决此问题,那么使用内联XML的含义是什么?诸如此类的性能,内存等如何显示:

Additionally, assuming no change in Scala to address this, what would be the implication of using inline XML? How would performance, memory, etc. with something like:

val countryCode = "FRA"
SQL(<c>        
  select * from Country c 
  join CountryLanguage l on l.CountryCode = c.Code 
  where c.code = {countryCode};
</c>.text)

推荐答案

如果您将来遇到这个问题,现在多行字符串插值已成问题.

If you are coming to this question from the future, multi-line string interpolation is now a thing.

val when = "now"
println(s"""this is $when a thing.""")
// this is now a thing

这篇关于Scala多行字符串占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆