Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala runtime string interpolation/formatting

Tags:

string

scala

Are there any standard library facilities to do string interpolation/formatting at runtime? I'd like the formatting to behave exactly the same as the macro based s"scala ${implementation} except that my string format is loaded at runtime from a config file.

val format = config.getString("my.key")
val stringContext = parseFormat(format)
val res = stringContext.f("arg1", "arg2")

with parseFormat returning a StringContext.

I imagine, worst case, I could just split the string on "{}" sequences and use the parts to construct the StringContext.

// untested
def parseFormat(format: String): StringContext =
  new StringContext("""{}""".r.split(format): _*)

Is there an obvious solution that I'm missing or would the above hack do the trick?

like image 674
Andy Avatar asked Nov 15 '25 18:11

Andy


1 Answers

There are no silly questions. Only Sunday mornings.

First, don't use String.format.

scala> val s = "Count to %d"
s: String = Count to %d

scala> String format (s, 42)
<console>:9: error: overloaded method value format with alternatives:
  (x$1: java.util.Locale,x$2: String,x$3: Object*)String <and>
  (x$1: String,x$2: Object*)String
 cannot be applied to (String, Int)
              String format (s, 42)
                     ^

scala> s format 42
res1: String = Count to 42

But formatting can be expensive. So with your choice of escape handling:

scala> StringContext("Hello, {}. Today is {}." split "\\{}" : _*).s("Bob", "Tuesday")
res2: String = Hello, Bob. Today is Tuesday.

scala> StringContext("""Hello, \"{}.\" Today is {}.""" split "\\{}" : _*).s("Bob", "Tuesday")
res3: String = Hello, "Bob." Today is Tuesday.

scala> StringContext("""Hello, \"{}.\" Today is {}.""" split "\\{}" : _*).raw("Bob", "Tuesday")
res4: String = Hello, \"Bob.\" Today is Tuesday.

It turns out that split doesn't quite hack it.

scala> StringContext("Count to {}" split "\\{}" : _*) s 42
java.lang.IllegalArgumentException: wrong number of arguments (1) for interpolated string with 1 parts
  at scala.StringContext.checkLengths(StringContext.scala:65)
  at scala.StringContext.standardInterpolator(StringContext.scala:121)
  at scala.StringContext.s(StringContext.scala:94)
  ... 33 elided

So given

scala> val r = "\\{}".r
r: scala.util.matching.Regex = \{}

scala> def parts(s: String) = r split s
parts: (s: String)Array[String]

Maybe

scala> def f(parts: Seq[String], args: Any*) = (parts zip args map (p => p._1 + p._2)).mkString
f: (parts: Seq[String], args: Any*)String

So

scala> val count = parts("Count to {}")
count: Array[String] = Array("Count to ")

scala> f(count, 42)
res7: String = Count to 42

scala> f(parts("Hello, {}. Today is {}."), "Bob", "Tuesday")
res8: String = Hello, Bob. Today is Tuesday

Hey, wait!

scala> def f(parts: Seq[String], args: Any*) = (parts.zipAll(args, "", "") map (p => p._1 + p._2)).mkString
f: (parts: Seq[String], args: Any*)String

scala> f(parts("Hello, {}. Today is {}."), "Bob", "Tuesday")
res9: String = Hello, Bob. Today is Tuesday.

or

scala> def f(parts: Seq[String], args: Any*) = (for (i <- 0 until (parts.size max args.size)) yield (parts.applyOrElse(i, (_: Int) => "") + args.applyOrElse(i, (_: Int) => ""))).mkString
f: (parts: Seq[String], args: Any*)String

or

scala> def f(parts: Seq[String], args: Any*) = { val sb = new StringBuilder ; for (i <- 0 until (parts.size max args.size) ; ss <- List(parts, args)) { sb append ss.applyOrElse(i, (_: Int) => "") } ; sb.toString }
f: (parts: Seq[String], args: Any*)String

scala> f(parts("Hello, {}. Today is {}. {}"), "Bob", "Tuesday", "Bye!")
res16: String = Hello, Bob. Today is Tuesday. Bye!
like image 54
som-snytt Avatar answered Nov 17 '25 09:11

som-snytt