Scala multiline string placeholder

非 Y 不嫁゛ 提交于 2019-12-14 02:18:54

问题


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

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

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

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")

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)

回答1:


A scala.xml.Elem would be constructed which had the string contents represented as an ArrayBuffer, chopped up for every { } substitution. I'm certainly no authority but I believe what would happen is that there's a little extra overhead in construction the object and then getting the children and concatenating them together at runtime but, at least in this example, as soon as it's passed to the SQL function which then extracts the string it wants (or perhaps this would be done with an implicit) the Elem object would be discarded so there'd be a little extra memory usage, but only briefly.

But in the bigger picture, I don't think it's performance that would hinder the adoption of this solution but I guess a lot of people would be uncomfortable abusing XML in this way by using a made-up tag. The problem would be with other users reading the code later trying to figure out the semantic meaning of the tag... only to find there isn't one.




回答2:


The example you give is almost certainly not doing string concatenation, it's creating parameterized SQL statements (probably via JDBC's PreparedStatement).

Ironically, the lack of easy string concatenation is probably slightly encouraging best practices in this case (although I certainly wouldn't use that as an argument either way on the topic).




回答3:


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


来源:https://stackoverflow.com/questions/5851475/scala-multiline-string-placeholder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!