Created
January 10, 2020 09:03
-
-
Save igstan/6c42cae2eee3a4ef449495fb001fd5c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class QuotedString(private val sc: StringContext) extends AnyVal { | |
/** | |
* {{{ | |
* scala> val msg = "foo" | |
* msg: String = foo | |
* | |
* scala> qs"$msg" | |
* res1: String = "foo" | |
* | |
* scala> qs"<$msg" | |
* res2: String = <"foo" | |
* | |
* scala> qs"<$msg>" | |
* res3: String = <"foo"> | |
* | |
* scala> qs"$msg>" | |
* res4: String = "foo"> | |
* }}} | |
*/ | |
def qs(vars: Any*): String = { | |
def interleave[A](xs: Iterable[String], ys: Iterable[Any]): String = { | |
val sbldr = new StringBuilder() | |
val xiter = xs.iterator | |
val yiter = ys.iterator | |
while (xiter.hasNext) { | |
sbldr ++= xiter.next() | |
if (yiter.hasNext) { | |
sbldr += '"' | |
sbldr ++= yiter.next().toString | |
sbldr += '"' | |
} | |
} | |
sbldr.mkString | |
} | |
interleave(sc.parts, vars) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment