Scala multiline string placeholder
Asked Answered
O

3

2

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)
Osis answered 1/5, 2011 at 21:17 Comment(1)
One drawback of the literal XML hack is that it is evaluated only one time. Also note that the enhanced string compiler plugin looks promising but with questionable maintenance: github.com/jrudolph/scala-enhanced-stringsOsis
C
0

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.

Convenient answered 1/5, 2011 at 21:34 Comment(0)
E
0

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

Earlineearls answered 2/5, 2011 at 4:30 Comment(1)
Please clarify how you see the lack of string concatenation encouraging best practices in this case.Osis
A
0

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
Algebra answered 31/1, 2019 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.