Skip to content

Commit ffa8e59

Browse files
committed
Fix implementation of f interpolation macro
1 parent e7eb6fd commit ffa8e59

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

library/src-bootstrapped/scala/internal/StringContext.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ object StringContext {
77
inline def f(sc: => scala.StringContext)(args: Any*): String = ${ fImpl('sc, 'args) }
88

99
private def fImpl(sc: Expr[StringContext], args: Expr[Seq[Any]]): Expr[String] = {
10-
// TODO implement f interpolation checks and addapt sc.parts
10+
// TODO implement f interpolation checks and generate optimal code
1111
// See https://github.com/alemannosara/f-interpolators-in-Dotty-macros
12-
'{ $sc.parts.mkString.format($args: _*) }
12+
'{
13+
// Purely runtime implementation of the f interpolation without any checks
14+
val parts = $sc.parts.toList
15+
assert(parts.nonEmpty, "StringContext should have non empty parts")
16+
val parts2 = parts.head :: parts.tail.map(x => if (x.startsWith("%s")) x else "%s" + x)
17+
parts2.mkString.format($args: _*)
18+
}
1319
}
1420

1521
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
abc
2-
Hello world
2+
Hello world!
3+
Hello world!

tests/run-with-compiler/f-interpolator.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
object Test {
33
def main(args: Array[String]): Unit = {
44
println(f"abc")
5-
println(f"Hello ${"world"}%s")
5+
println(f"Hello ${"world"}%s!")
6+
println(f"Hello ${"world"}!")
67
}
7-
}
8+
}

0 commit comments

Comments
 (0)