The Java (and for other languages) library “StringTemplate” is a string-templating library that focuses on language-independent templating, and gets used in the ANTLR parser generator for its test suite; probably in other places too.
The answer is probably “huh, what’s that?” but I figured I’d ask anyway:
Does anybody care about this in this neighborhood? I’m asking b/c I’m starting to do OCaml implementation, and, well, it’s always useful to know whether anybody else might care.
let template name id = [%string
"Your name is %{name#SingleQuoted}. Your ID is %{id#Int}."] ;;
val template : string -> int -> string = <fun>
template "O'Connor" 301 ;;
- : string = "Your name is 'O\'Connor'. Your ID is 301."
where SingleQuoted is a module with a function to_string of type string -> string.
I skimmed JEP 430 and it seems like the focus is on “safety”, in the sense of enforcing specific formattings for arguments (such as parameters to SQL statements that need escaping). It occurs to me that PPXes could offer similar safety if they gained a way to constrain placeholders to a set of formatter modules inside some namespace.
A hypothetical syntax:
module Safe = struct
module SingleQuoted = struct
let to_string s = ...
end
end
let template name job = [%string Safe
"Your name is %{name#SingleQuoted}. Your job is %{job#Unquoted}."] ;;
Error: Unbound module Safe.Unquoted
But it’s possible that I’ve missed some other feature of StringTemplate that makes it compelling.