In python, one can use printf like formatting with the “%” operator:
"i am %d years old" % 99
or
"%s is %d years old" % ("bob", 101)
Is there a way to get the same concise syntax in Ocaml, for arbitrary numbers of arguments?
For a single argument, the following works:
let (%) = Printf.sprintf in ... "i am %d years old" % 99
Is there a way that works for arbitrary numbers of arguments?
In theory, it does not seem any more difficult to use a format to produce the type
(typ1 * typ2 * ... * typn) -> stringthantyp1 -> typ2 -> ... -> typn -> string. That is, perhaps, with the exception of recursive formats%( fmt %). Does anyone actually use those?In practice, though, the OCaml implementors chose the latter form, and implemented typesystem hacks for that form, not for the former one. So I am afraid the answer is that short of patching the compiler, you’re stuck with the curried form of format string substitution.