Skip to content

Instantly share code, notes, and snippets.

@bmc
Created January 28, 2013 14:04
Show Gist options
  • Save bmc/4655741 to your computer and use it in GitHub Desktop.
Save bmc/4655741 to your computer and use it in GitHub Desktop.
Making your own Scala 2.10 string interpolator
implicit class UpCaser(val sc: StringContext) extends AnyVal {
def u(args: Any*): String = {
val strings = sc.parts.iterator
val expressions = args.iterator
var buf = new StringBuffer(strings.next)
while (strings.hasNext) {
buf append expressions.next
buf append strings.next
}
buf.toString.toUpperCase
}
}
println(u"this will be all upper case") //=> "THIS WILL BE ALL UPPER CASE"
// See http://docs.scala-lang.org/overviews/core/string-interpolation.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment