Skip to content

Instantly share code, notes, and snippets.

@jstrachan
Created March 14, 2012 11:07
Show Gist options
  • Save jstrachan/2035806 to your computer and use it in GitHub Desktop.
Save jstrachan/2035806 to your computer and use it in GitHub Desktop.
kotlin code:
val formatter = HtmlFormatter(usersLocale).useLongDates()
"foo $a bar $b".toHtml(appender, formatter)
by default is turned into...
StringTemplate(array("foo ", " bar"), array(a, b)).toHtml(appender, formatter)
if a custom compiler plugin knew you wanted a Html template, say, it could change that code to
MyHtmlTemplate(a, b).toHtml(appender, formatter)
with this class being generated:
class MyHtmlTemplate(a: String, b: Foo) : HtmlTemplate {
fun toString() = "foo " + a + " bar " + b
fun toHtml(out: Appender, format: HtmlFormat): Unit {
out.append("foo ")
format.append(out, a)
out.append(" bar ")
format.append(out, b)
out.append(" bar ")
}
}
A really smart compiler plugin could just do this to avoid any object construction :)
appender.append("foo ")
format.append(appender, a)
appender.append(" bar ")
format.append(appender, b)
appender.append(" bar ")
though its a bit more complex - e.g. there may be a few toHtml() methods - whether or not you pass an Appender or just want a String being returned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment