Custom String Templates

Yes, the compile-time aspect is secondary. It would be nicer for performance reasons, but all of the use-cases that our team has would be just as well served by a runtime solution.

The true key is the syntactic clarity and type information at the point of applying the template.

If you can map:

abc"Interpolate this: ${a} and ${b}."

to:

templateHandler(“abc”, arrayOf<String>("Interpolate this: ", “ and ”, "."), arrayOf<Any>(a, b))

Then you are able to do what is needed.

I believe that this is a widely used feature in Scala and there are numerous DSLs built around it. We have built four internally-used DSLs based on this capability and it leads to much more concise and safe code than any other approach to this problem.

2 Likes